Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a mult-line file, with segments delimited by "%" (one or more on a newline by itself). So for example:
part1
%%%%%%
part2
%%%%%%
part3
this is still part 3, even though we have a % in it somewhere
the % must be on a newline, like so:
%
So that would have 3 "segments". I want to store this in an array. I think a good way to do this would be to use 'split' (if you can think of a better way, please tell me). I've tried (assume FILE is already open, etc):
@list = split /.+(%+)/s, <FILE>;
But it only gets the first line. How would I code this properly? TIA

Replies are listed 'Best First'.
RE: using split on a file
by h0mee (Acolyte) on Feb 15, 2000 at 00:52 UTC
    You should also be able to turn off what perl considers a newline by setting
    $/ = undef;
    Now you can
    $string = <FILE>; #since $/ is off, everything #gets slurped into $string @array = split /%%%%\n/m, $string; #now we just split on #the appropriate delimiter, carriage #return included
Re: using split on a file
by Anonymous Monk on Feb 14, 2000 at 11:17 UTC
    You can try something like this: LOOP: while (<INFILE>) { next LOOP if /^%/; #process segment here } That'll process for any line that doesn't start with a %. Egg, YAPH-and-BOFH-wannabe
      D'oh! Sorry about the lack of formatting on that one. It should have read

      LOOP:  while (<INFILE>) {
          next LOOP if /^%/;
          #process segment here
      }
      

      Hope this helps, and that it shows up right this time.

      Egg, YAPH-and-BOFH-wannabe

Re: using split on a file
by jamescohen (Initiate) on Feb 14, 2000 at 18:42 UTC
    Haven't tested it but this should work

    Using a filehandle (as you were with <FILE>) only returns a line at a time.

    my ($allsource) ; while <FILE> { $allsource .= $_ ; } # Now $allsource contains all of the lines of the file my (@segments) = split /%{5}/, $allsource ; # Each element in @segments is one of the segments # Could undefine $allsource to free a little memory undef ($allsource) ;
Re: using split on a file
by Anonymous Monk on Feb 15, 2000 at 11:43 UTC
    You can try something like this: LOOP:
    while (<INFILE>) { next LOOP if /^%/; #process segment here }
    That'll process for any line that doesn't start with a %. Egg, YAPH-and-BOFH-wannabe
Re: using split on a file
by Anonymous Monk on Feb 13, 2000 at 06:52 UTC
    How about: @list = split /^%{5}$/m, <FILE>
Re: using split on a file
by lolindrath (Scribe) on Jan 02, 2001 at 10:27 UTC
    Ok, this is what worked for me (adapted to your code a little). I was using CGI.pm and when I just undefined $/ it really screwed some stuff up.
    my $OldNewLine = $/; $/ = undef; open( FILE, "file" ); my @List = split( /\n%{5}\n/, <FILE> ); close (FILE); $/ = $OldNewLine;
    Update: Ignore the above code look at the reply below this.

    --=Lolindrath=--

      Or, you can use a block. It's neater.

      my @List=(); { local $/ = undef; open( FILE, "file" ); @List = split( /\n%{5}\n/, <FILE> ); close (FILE); } # $/ is back to what it used to be!