in reply to Re: Parsing multiline string line by line
in thread Parsing multiline string line by line

If you're slurping, might as well assign to an array:

$/ = undef;

open "FIN", "<raja.txt" or die "cant open :$!";
my @fin = <FIN>;
close FIN;

print for @fin;
  • Comment on Re^2: Parsing multiline string line by line

Replies are listed 'Best First'.
Re^3: Parsing multiline string line by line
by hbm (Hermit) on Feb 19, 2009 at 21:36 UTC

    But that puts the whole file into the first element of the array. (Change your print to print "|$_|\n" for @fin; and you'll see only one element.)

    You probably want this:

    $/ = undef; open "FIN", "<raja.txt" or die "cant open :$!"; my @fin = split/\n/,<FIN>; close FIN; print "|$_|\n" for @fin;

    Or to also get rid of blank lines:

    ... my @fin = grep {$_} split /\n/, <FIN>;

    Update: Square brackets in my original post created a link I didn't intend. I changed them to pipes. And alternatively, you could just print scalar @fin to confirm how many items you slurped.

      Thank you for the correction! Odd that there are several examples of the array assignment floating around the web and I filed it away as valid without testing.