Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Hash table manipulation

by GertMT (Friar)
on Jul 12, 2011 at 06:21 UTC ( #913853=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Hash table manipulation
in thread Hash table manipulation

I tried to get the first part of this code working but wasn't successful (see below). I had a problem using the map-function.
Can someone explain me why it doesn't work as presented here?
Thanks,

#!/usr/bin/perl -w use strict; #define your data: my %hash = ( .1 => 'url1', .2 => 'url2', .3 => 'url3', .7 => 'url7', .9 => 'url9', ); ## It works if I use this loop # my @good_keys; # foreach my $key ( keys %hash ) { # if ( $key > .4 ) { # push @good_keys, $key; # } # # } # but I can not get it to work with this map-function #define the fitness function / match determination my $good_value = sub { $_ > 0.4 }; #find the keys that match the fitness function: my @good_keys = map { $good_value->($_) } keys %hash; #now do something with the keys: ##eg. print the values, in hash order: for my $key (@good_keys) { print $hash{$key}, "\n"; }


Comment on Re^3: Hash table manipulation
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-12-26 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls