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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.