in reply to taking sections out of a file and put it in an array

my @arr; while (<FILE>) { push @arr, $1 if /\$FILES(.*?)\$/; }
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: taking sections out of a file and put it in an array
by Chief of Chaos (Friar) on Dec 06, 2002 at 09:00 UTC
    starts with $FILES and ends with $ :
    my @arr; while (<FILE>) { push @arr, $1 if /^\$FILES(.*?)\$$/; }
    (inserted '^' and '$' to mark start and end)

      Check juo's example, there will be more data after the $ so you'll need to leave of the second $ (as davorg did) or it won't match.

      open DATA, "junk.txt" or die $!; my @arr; while (<DATA>) { # add the extra $ and it won't print anything push @arr, $1 if /\$FILES(.*?)\$/; } close DATA; print $_, "\n" for @arr;

      Try it out.

      Update: I think I misinterpreted Chief of Chaos's post. If you want to ensure there's no data before the $FILE or after the $ then he is correct.

      Update: If you want each CSV as an individual array element, substitute the push line above with:

      push @arr, split(/, /, $1) if /\$FILES(.*?)\$/;

      Otherwise they'll all read into one element.

Re: Re: taking sections out of a file and put it in an array
by juo (Curate) on Dec 09, 2002 at 10:46 UTC

    The problem is that it assumes that everything is on one line while the text is on multiple lines so it has to keep putting it in the array unless the line contains the $. I have tried to use while in the while loop but it gets out of memory. Is their an easy way when a line is found to keep adding the lines in the array untill something else is found in a next line.