Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: from array to hash with grep

by graq (Curate)
on Jun 15, 2006 at 08:12 UTC ( [id://555444]=note: print w/replies, xml ) Need Help??


in reply to from array to hash with grep

I expect there are plenty of 'use map!' replies before I finish this, but I'll throw my torch light on the subject.

$inp{$_} = 1 grep ( /\w{3}_(\w)/, @inp ) ;

This is initially syntactically wrong. You have a list and need to iterate over it somehow.

$inp{$_} = 1 for grep ( /\w{3}_(\w)/, @inp );

However grep returns the line that matched, not what you 'captured' in $1, so you will match all lines with no change.

Hence you need to use map.

$inp{$_} = 1 for map ( /\w{3}_(\w)/, @inp );

-=( Graq )=-

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found