Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie help - finding all substrings with index?

Reply
Thread Tools

newbie help - finding all substrings with index?

 
 
kadau
Guest
Posts: n/a
 
      03-25-2005
Hi

I hope this is an appropriate group to post in, my apologies if it is
not. I would to to be able to read from a file and be able to locate all
the instances of a substring within it, save it in an array, then print
it out. I am fine with this except finding all the matches. Could anyone
explain how to do this with index? Or is there some other way (please
try to keep it simple if you can
Here is the text file ive been given

__

#Transaction code

S,M,L

#Sales commission

5:7:10

#Retail price items

*1<1002.00<
*2<125.00<
*3<61864.35<
*4<890876.99<
*5<9.99<

__

I want to locate & save to an array all lines begging with "*". I
presume I will then be able to use split to get the raw prices out of
the array (in between the "<").

Thanks to anyone who can help.
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-25-2005
kadau <brettaw@NO_spam_gmail.com> wrote in news:MPG.1caea6738a31c9e8989680
@news.tpg.com.au:

> it out. I am fine with this except finding all the matches. Could
> anyone explain how to do this with index? Or is there some other
> way (please try to keep it simple if you can


What have you tried?

Have you read the documentation?

perldoc perlre
perldoc perlretut

Please consult the posting guidelines for this group for information on
how to help others help you.

Sinan
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      03-25-2005


kadau wrote:

> I hope this is an appropriate group to post in, my apologies if it is
> not. I would to to be able to read from a file and be able to locate all
> the instances of a substring within it, save it in an array, then print
> it out.


Are you sure that's _what_ you want to do and not _how_ you think you
can do something? Anyhow I think you are expressing what you want
carefully. In programming, precisely understanding what it is you want
to do is often 90% of doing it.

> I am fine with this except finding all the matches. Could anyone
> explain how to do this with index?


To do that you'd need to slurp the whole file into a string (see FAQ)
but this is far form a natural approach.

my $pos=0;
while ( ( $pos = index( $file_content, $target, $pos )) > -1 ) {
# Do stuff with $file_content and $pos
}

> Or is there some other way (please
> try to keep it simple if you can


Yes there are much simpler ways. But first you need to step back and
consider what you really want to do.

> I want to locate & save to an array all lines begging with "*".


Hmmm... ok now we are perhaps getting closer.

my @lines_starting_with_asterisk;
local *_;
while(<FILE>) {
push @lines_starting_with_asterisk => $_ if /^\*/;
}

> I
> presume I will then be able to use split to get the raw prices out of
> the array (in between the "<").


But why do you think you need an array? Can you not simply process the
stuff as you read it from the file?

The split() function is good if you have a delimited list of arbitrary
length but the usual way to structured text in Perl is just the simple
pattern match. Simply write a pattern that matches the lines you are
interested in and captures the interesting parts of the line.

local *_;
while(<FILE>) {
next unless my ($item, $price)=/^\*(.*?)<(.*?)</;
# Do stuff with $item and $price.
}

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-25-2005


Brian McCauley wrote:

> Anyhow I think you are expressing what you want carefully.


Opps, should have said "... _not_ expressing...".

Gee, that's kinda embarrasing.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get all possible substrings Robert Reno C++ 4 01-28-2010 12:46 PM
Finding and Replacing Substrings In A String DarthBob88 C Programming 7 09-23-2007 03:14 PM
Generating all ordered substrings of a string girish@it.usyd.edu.au Python 8 07-13-2006 07:41 AM
enumerate all adjecent substrings in the file puzzlecracker Perl Misc 9 12-13-2005 10:36 AM
Binary files, substrings and (un)packing. Leandro Pardini Perl 1 10-27-2003 07:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57