Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Confusing grep behavior

by dextius (Monk)
on Dec 04, 2007 at 21:09 UTC ( #654950=perlquestion: print w/replies, xml ) Need Help??
dextius has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to understand two snippets of code, that have different behaviors that I do not immediately grasp. Consider:
use strict; use Data::Dumper; my @res = grep $_ split(",", "a,b,c,d,,f,,h"); print Dumper(\@res); my @res2 = grep { $_ } split(",", "a,b,c,d,,f,,h"); print Dumper(\@res2);
When ran, it returns
$VAR1 = [ 'a', 'c' ]; $VAR1 = [ 'a', 'b', 'c', 'd', 'f', 'h' ];
I understand the second example, as I am evaluating the "truth" of the elements I am iterating over. I do not understand the output of the first dump, when given just "$_". I did look at grep's perldoc, hoping for an ounce of clairvoyance, but to no avail.

Replies are listed 'Best First'.
Re: Confusing grep behavior
by FunkyMonk (Canon) on Dec 04, 2007 at 21:16 UTC
    The first is a syntax error which caused the perl on my machine (Activeperl 5.8.8 build 822) to spit out:
    panic: ck_grep at C:/Documents and Settings/bri/workspace/PerlMonks/te line 10 (#1) (P) Failed an internal consistency check trying to compile a grep. Uncaught exception from user code: panic: ck_grep at C:/Documents and Settings/bri/workspace/PerlMonk +s/ lin e 10. at C:/Documents and Settings/bri/workspace/PerlMonks/ line 10

    What did you really mean it to say?

      This code does not have any issues in v5.8.6 on Mac OS X.
Re: Confusing grep behavior
by grep (Monsignor) on Dec 04, 2007 at 21:19 UTC
    You need to reread grep
    my @res = grep $_ split(",", "a,b,c,d,,f,,h"); #is a syntax error.

    According to the POD when grep is not called with a block it requires a comma before the list you are working on.
    grep EXPR,LIST

    So this will work as you expect.

    my @res = grep $_, split(",", "a,b,c,d,,f,,h");
    One dead unjugged rabbit fish later...
      You are quite right.. I should rephrase my question... On Mac OS X, the comma isn't required, and has far different behavior. Anyone happen to know what that is?
        It's not a Mac thing. 5.8.5 on Linux allows it as well. I'm guessing it's a bug that was fixed in 5.8.7 or 5.8.8.

        No matter what OS you run it on the comma is still required, as noted by the POD and (quite noticeably) by the incorrect performance. The only difference(bug) is that it didn't throw an error. A crime is still a crime, whether you get caught or not :).

        One dead unjugged rabbit fish later...
Re: Confusing grep behavior
by lodin (Hermit) on Dec 04, 2007 at 21:17 UTC

    You're missing a comma after $_. It should be

    grep EXPR, LIST


Re: Confusing grep behavior
by cdarke (Prior) on Dec 05, 2007 at 10:25 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://654950]
Approved by grep
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-05-01 03:13 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (544 votes). Check out past polls.