Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

m{} within grep{} causing problems? (code)

by deprecated (Priest)
on Jun 25, 2001 at 19:39 UTC ( [id://91311]=perlquestion: print w/replies, xml ) Need Help??

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

die "malformed index file!" unless scalar ( grep { $_ =~ m[\d{3}-\d{3}] }, values %batch_ids ) == 6;
is returning this error:
syntax error at readmagazine.pl line 21, near "},"
where line 21 is the grep. Any ideas what the problem is? My suspicion is that the \d{3} is conflicting with grep's {}. Other than that, the syntax seems to be correctly formed to me...

thanks
brother dep.

--
Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
Re: m{} within grep{} causing problems?
by japhy (Canon) on Jun 25, 2001 at 19:43 UTC
    The grep BLOCK LIST form takes no comma after the block -- you have a comma there. You could use the expression syntax: <code> die "..." unless grep(/\d{3}-\d{3}/, values %hash) == 6;

    japhy -- Perl and Regex Hacker
Re: m{} within grep{} causing problems?
by mikfire (Deacon) on Jun 25, 2001 at 19:48 UTC
    You are confusing perl. According to perldoc -f grep, you have two choices:
    grep BLOCK LIST grep EXPR,LIST
    Your usage is a hybrid of the two, as the curly braces indicate the beginning of a block. Both of these two forms work
    scalar( grep m[\d{3}-\d{3}], values %batch_ids ) == 6;
    or
    scalar( grep { m[\d{3}-\d{3}] } values %batch_ids ) == 6;

    mikfire

Re: m{} within grep{} causing problems?
by btrott (Parson) on Jun 25, 2001 at 19:44 UTC
    Your problem is that the grep form that you're using, grep BLOCK LIST, does not use a comma (',') after the BLOCK. Take out the comma and it will compile.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found