Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Parsing multiline string line by line

by Jayson (Novice)
on Feb 19, 2009 at 19:54 UTC ( #745180=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://745180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-09-20 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?