in reply to Parsing multiline string line by line

Is this something you folks need
$/ = undef;#set input record seperator to null[default \n] open("FIN","<raja.txt") or die "cant open :$!\n"; $content = <FIN>; #Whole file content in a single scalar variable close(FIN); foreach $line (split /\n/ ,$content) { print $line; }

Replies are listed 'Best First'.
Re^2: Parsing multiline string line by line
by flamey (Scribe) on Feb 19, 2009 at 15:05 UTC
    hmm, this looks prettier than what perreal suggested above. thanks!
Re^2: Parsing multiline string line by line
by Jayson (Novice) on Feb 19, 2009 at 19:54 UTC
    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;

      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.