Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Arrays and Hashes Mapping

by jesuashok (Curate)
on Dec 08, 2004 at 10:34 UTC ( [id://413146]=perlquestion: print w/replies, xml ) Need Help??

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

@list = ( one , two , three ); %hash = ( one => 1 , two => 2 , three => 3 ); foreach (@list) { if ( $hash{$_} ) { $_ = '' ; } }
I want the above method to be simplified by using map or something else But it should be simple ( i,e ) one line. If the key is available in hash then the value should be removed from the list
Anthony Jesu Ashok Majoris Systems Ltd Bangalore

2004-12-08 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Arrays and Hashes Mapping
by pingo (Hermit) on Dec 08, 2004 at 11:15 UTC
    If I understand you correctly, perhaps something like this:

    @list = ('one' , 'two' , 'three'); %hash = ( one => 1 , two => 2 , three => 3 ); @list = grep { !$hash{$_} } @list;
      Better use exists here, so that ( zero => 0, empty => '' ) won't give you false positives:
      @list = grep { !exists $hash{$_} } @list;
      --kap

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found