Question about implementing range operator with strings

B

bayxarea-usenet

I had a routine that worked fine with a range operator while looping
through a filehandle... but I have since changed the data source and it
is now already in an array... and the range operator does not appear to
be working...

I am basically extracting lines of data from a larger set.


--------------------
before -------------

open(IN,"< $file");

while (<IN>) {
if (/$start_string/ .. /$end_string/) {
if (($_ !~ /$start_string/) && ($_ !~ /$end_string/)) {
....
.... do something with $_ ...
....
} # end if
} # end if
} # end while
close IN;


--------------------
now -------------


@data_from_file; # lines of data retrieved elsewhere

foreach (@data_from_file) {
if (/$start_string/ .. /$end_string/) {
if (($_ !~ /$start_string/) && ($_ !~ /$end_string/)) {
....
.... do something with $_ ...
....
} # end if
} # end if
} # end foreach


Also -- there are now multiple places within the @data_from_file array
that I want to extract the data for which I am searching.
So want to be able to go inside (between the start and end strings)
each time the pair occurs within the array.

I thought of using something like this:

while ($_ = shift @data_from_file) {
if (/$start_string/ .. /$end_string/)
....
.... etc

but it did not work either.

After reading the docs I thought perhaps a slice would work - but I am
not sure how to tie the range to the elements of the array.

@data_I_want = (/$start_string/ .. /$end_string/) # ????

Thanks for any clues as to how to change this to work from a while loop
on the file handle to looping through each element of the array.

And - as I mentioned - I need to do this multiple times as the block I
am searching for will occur several times - so ... multiple slices on
the same array? Is that possible?

Thanks,

John
 
B

bayxarea-usenet

Thanks for your reply.

After seeing that it worked for you - I looked further into the data as
it coming into the function I wrote.
As it turns out, the data included \r s instead of \n s as line
seperators - so I didn't have the lines as elements as I thought I did.

I changed my split to (@data_from_file) = split /\n|\r/,$data

and now it has the lines - and the range operator does work as you
suggested.

Thanks!


John
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top