http://qs321.pair.com?node_id=913853


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"; }