Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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?
#!/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"; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-10-20 12:55 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (81 votes). Check out past polls.