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

Hi, i'm struggling with a stupid issue. I'm trying to split a chunk of text in a scalar $text into a list of lines @lines (obviously splitting on the newline char). Having trouble telling split howto do it, I would have thought: my @lines = split(/\n/, $text); would do the trick but its splitting on whitespace?!? Hmm, how to do this? Thanks!

Replies are listed 'Best First'.
Re: dumb split question
by BrowserUk (Patriarch) on Mar 24, 2003 at 00:45 UTC

    split /\n/ works fine for me, there must be more to your problem than you are identifying?

    #! perl -slw use strict; my $text = <<EOT; this is line 1 this is line 2 this is line 3 this is line 4 this is line 5 EOT my @lines = split /\n/, $text; print for @lines;

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: dumb split question
by tachyon (Chancellor) on Mar 24, 2003 at 00:55 UTC
    my @lines = split "\n", $text; is right. Note"\n" and /\n/ are the same. It WILL be splitting on the newlines (only) If it is splitting where you don't want it to then you have extra newlines which you just THINK are whitespace. You might get this misconception if this is say CGI related as in HTML all 'whitespace' ie \t \n ' ' gets rendered as a single space.
    my $text = "foo 1\t2 3\nbar 1\t2 3\nbaz 1\t2 3"; my @lines = split "\n", $text; print join '|', @lines; # lets see what $text actually contains by displaying # non word digit space chars a (\octal) tokens $text =~ s/([^\w\d ])/sprintf "(\\%03o)", ord $1/ge; print "\n\nActual text:\n", $text; __DATA__ foo 1 2 3|bar 1 2 3|baz 1 2 3 Actual text: foo 1(\011)2 3(\012)bar 1(\011)2 3(\012)baz 1(\011)2 3

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: dumb split question
by Popcorn Dave (Abbot) on Mar 24, 2003 at 04:36 UTC
    It's not a dumb question at all, and I ran in to the same problem once. I was splitting HTML text and pulled my hair out for a while trying to figure out why it wasn't putting the slurped text in to an array using @text=split(/\n/, $slurp;

    The problem seems to stem from the differences between Unix and Windows ( and possibly the Mac too, I'm not sure ) and how they write newlines to a file.

    I solved the problem by splitting on \r or \r\n or \n, depending on how the newlines were written in the file.

    Hope that helps!

    There is no emoticon for what I'm feeling now.

Re: dumb split question
by DarknessX (Scribe) on Mar 24, 2003 at 00:46 UTC
    not quite sure why yours didn't work, unless you used it incorrectly.
    #!/usr/bin/perl -w use strict; my $text="blah blah\nblah\nblah\nblah\nblah\nyadda yadda yadda\n"; my @lines=split /\n/, $text; #replace with split(/\n/,$text); at will print "This: $_\n" for @lines;
    -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GPA/P/S d+ s++:+ a--- C++ UL++ P++>++++ L++ E W++ N o? K? w-- !O M- V? + PS++ PE- Y PGP- t 5- X+ R* tv-- b++ !DI D---(+) G e- h-- r++ y? ------END GEEK CODE BLOCK-----
    UPDATE: Minor cosmetic change at tach's suggestion

      Why not use trailing operators?

      print "This: $_\n" for @lines;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print