Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Arrays and Hashes Mapping

by jesuashok (Curate)
on Dec 08, 2004 at 10:34 UTC ( #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
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? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2016-10-01 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (8 votes). Check out past polls.