newbie help - finding all substrings with index?

K

kadau

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.
 
A

A. Sinan Unur

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
 
B

Brian McCauley

kadau said:
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.
}
 
B

Brian McCauley

Brian said:
Anyhow I think you are expressing what you want carefully.

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

Gee, that's kinda embarrasing. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top