in reply to RE: A splitting question
in thread A splitting question

The \n is only at the end of a line, so you won't have any string there after the split.

Update: Some sample code to demonstrate:

#!/usr/bin/perl -w use strict; while (<DATA>){ for my $myword (split){ print "[$myword]\n"; } print "we have a newline now\n"; } __END__ a b c d e f g h i
Outputs:
[a] [b] [c] we have a newline now [d] [e] [f] we have a newline now [g] [h] [i] we have a newline now
It never splits on the newline (It tries, see that there are only empty elements afterward, and discards it)

Replies are listed 'Best First'.
split and newlines in RE:{3} A splitting question
by jeroenes (Priest) on Nov 15, 2000 at 17:15 UTC
    When I read these posts at first, I misunderstood the results of split. Before yesterday, I didn't realize that plain split also splits on newlines (although I knew!). So, the comment I wanted to make was, do a chop first. But of course that is unnessecary.

    If you want it in an oneliner, you could just say: (The use of map releaves perl of doing an itteration.)

    while(<>) {map { print "$_\n"} split; print "newline\n"}

    If you combine this with little's post, you would get something like

    undef $/; $_=<>; s/\n/ newline /g; map{ print "$_\n"} split;
    Warning: Do not try to do something with $_ after the map, that doesn't work...

    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

RE: RE: RE: A splitting question
by BigJoe (Curate) on Nov 15, 2000 at 03:04 UTC
    The sample code helped a ton. It now works perfectly. Thanks

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.
      If you only want to count the lines then use something like
      my @myfile = <File>; my $lines = @myfile;
      but that was just guessing what you are doin.
      But to go on with your idea you need to
      undef $/; # enable "slurp" mode $_ = <FH>; # whole file now here
      which is an excerpt from perlman:perlvar

      Have a nice day
      All decision is left to your taste