in reply to A splitting question

I want it to split on both \n and \s. I just want to know when it actually splits on a \n.

--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

Replies are listed 'Best First'.
RE: RE: A splitting question
by swiftone (Curate) on Nov 15, 2000 at 02:52 UTC
    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)
      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)

      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