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

I have been looking for an answer for this for a couple hours. Using this code is there a way to find out what character the file was split as. I need to know if it was split on a \n or a \s.
while (<FILE>) { for my $myword (split){ if ($/ eq "\n") { #do stuff } #do more stuff } }
It is probably very simple I just can not see it. I have tried the $/ but that evaluates true every time. If I use single quotes it is always false. Any ideas or a point in the right direction would greatly be appreciated.

--BigJoe

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

Replies are listed 'Best First'.
Re: A splitting question
by swiftone (Curate) on Nov 15, 2000 at 02:37 UTC
    I don't think you can do it as you are trying. According to perlvar, most of the $thingy variables for regexes don't count for regexes within a block, which is how I imagine split() treats them.

    The <FILE> should make it so that the newline is always and only at the end. (unless you changed $/)Why not:

    while(<FILE>){ for my $myword (split(/\s+/)){ #do stuff } #There's a newline now }

    Update: BTW, $/ will always equal "\n" unless you change it. It's the value that the <FILE> constructor uses to split on, not what split uses.

RE: A splitting question
by little (Curate) on Nov 15, 2000 at 02:42 UTC
    $/ is the input record separator, newline by default
    So of course it matches, did you mean something different? Did you want to compare $_ ?

    Have a nice day
    All decision is left to your taste
RE: A splitting question
by japhy (Canon) on Nov 15, 2000 at 03:27 UTC
    You can also find out what you split on:
    $str = "what's up, doc?"; for (split /(\W+)/, $str) { print "<$_>\n"; } __END__ <what> <'> <s> < > <up> <, > <doc> <?>


    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: A splitting question
by BigJoe (Curate) on Nov 15, 2000 at 02:47 UTC
    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.
      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.