Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

why isn't split() working here?

by 7stud (Deacon)
on Dec 09, 2010 at 03:25 UTC ( #876157=perlquestion: print w/replies, xml ) Need Help??

7stud has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; $/ = undef; my $whole_file = <DATA>; print $whole_file; print "-" x 20, "\n"; my @sections = split /xxx\n/, $whole_file; print $sections[0]; print "-" x 20, "\n"; print $sections[1]; #line 14 __DATA__ aaa bbb ccc xxx yyy zzz --output:-- aaa bbb ccc xxx yyy zzz -------------------- aaa bbb ccc xxx yyy zzz -------------------- Use of uninitialized value in print at t.pl line 14, <DATA> chunk 1.

This works:

use strict; use warnings; my $string = "abc\nxyz"; my @arr = split /\n/, $string; print "$arr[0]\n"; print "$arr[1]\n"; --output:-- abc xyz

Replies are listed 'Best First'.
Re: why isn't split() working here?
by roboticus (Chancellor) on Dec 09, 2010 at 03:39 UTC

    7stud:

    If you set $whole_file = "aaa\nbbb\nccc\nxxx\nyyy\xzzz"; in your first example, you'll see that it works there, too. I'd guess that you're on a windows box and your DATA section actually looks like: "aaa\r\nbbb\r\nccc\r\nxxx\r\nyyy\r\nzzz\r\n". If that's true, then changing your split expression to /xxx\r?\n/ or /xxx[\r\n]+/ might save the day for you.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Or if you are running 5.10 or newer:
      split /xxx\R/
      Thanks. I was using codepad.org to run some code, and per your suggestion if I switch \n to \r\n, then my code works. I was using codepad.org to run some code using a windows computer, but I still don't quite understand why perl doesn't seemlessly translate \r\n to a \n. When I hit return on a windows computer, I should be entering \r\n--not just \r.
        Perl does seamlessly convert \r\n to \n on Windows unless you binmode. It would be nice if there was a layer that handled common line endings seamlessly, but noone's gotten around to doing it. It's not that hard, even.
        I still don't quite understand why perl doesn't seemlessly translate \r\n to a \n.

        Presumably, the perl that codepad.org is running is a Unix-built perl, which means it does not add a :crlf PerlIO layer to file handles by default (as is done on Windows). The :crlf layer is responsible for transparent \r\n <—> \n linefeed translations (see PerlIO).

        Adding binmode DATA, ":crlf" before slurping in the data should emulate Windows perl behavior on codepad.org.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-10-16 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (69 votes). Check out past polls.

    Notices?