Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Howto convert lines in stringified text into element of an array

by GrandFather (Saint)
on Sep 04, 2007 at 03:48 UTC ( [id://636823]=note: print w/replies, xml ) Need Help??


in reply to Howto convert lines in stringified text into element of an array

use warnings; use strict; my $file = <<'FILE'; foo bar qux foo foo foo FILE my @lines = split $/, $file; print "$_\n" for @lines;

Prints:

foo bar qux foo foo foo

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Howto convert lines in stringified text into element of an array
by ikegami (Patriarch) on Sep 04, 2007 at 03:55 UTC
    Your solution truncates all trailing blank lines. That might be acceptable, but I'm mentioning it to let the OP (and other readers who might use this code) decide.
    my $file; my @data_lines; while (<DATA>) { $file .= $_; chomp; push @data_lines, $_; } my @split_lines = split $/, $file; print(scalar(@data_lines), "\n"); # 3 print(scalar(@split_lines), "\n"); # 2 __DATA__ foo bar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-25 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found