Konrad Foerstner wrote:
> Hi folks,
>
> an new question about the mystery of regexs:
>
> I want to store some parts of a file wich are separetet by "#". Each
> each part should become an entry of an array. example:
>
> "
> # foobar
> nothing important
> nothing interesting
> # bar foot
> lululul
> lalala
> lalala
> # foobar2
> Rumba-Samba-Tango
> "
>
> (okay it stop before the example becomes to silly :) )
>
> $array[0] should contain:
> "
> # foobar
> nothing important
> nothing interesting
> "
>
> Well, my code looks like this:
>
> open(INPUT,$input_file);
> while (<INPUT>) {
> if (/#/ .. /\n#/) {
> push @array, $_;
> }
> }
>
> But now every line is stored with an own index. I can't use brackets
> to get the group of lines in $1. Then I get an error message.
>
> Any hint?
Hi Konrad. I don't think this is a case for contorted regexes or even
the range operator. How about this:
use strict;
use warnings;
@ARGV = $input_file;
my @array;
my $i = 0;
while (<>) {
if (/^#/) {
$i++ if @array;
}
$array[$i] .= $_;
}
I don't think there's anything obscure here except for testing the array
to make sure $i isn't incremented before the first block.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]