in reply to Hash table manipulation
my @filtered_keys = grep { $_ > 0.4 } keys %hash; foreach $key ( @filtered_keys ) { # Do something with $hash{$key} , which will be the # url's pertaining to each of the keys whos numeric # value is greater than 0.4. }
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash table manipulation
by spazm (Monk) on Jul 11, 2011 at 21:52 UTC | |
David's post works, given my understanding of the orginal post. Original poster, your question would be helped by example data. Here is the assumption we are working with. 1) Your data set contains keys which are floats and values containing URL strings. e.g.
2) you wish to extract the values for which the keys meet certain properties, in your example "key > 0.4" 3) you wish to do something with the values and/or keys that match the properties. "Now, what modification i want is, i just want to filter the hash keys for example, i want only the values greater than 0.4 and its correct url as a output rather than all random numbers in key.." Put that all together:
Why the anonymous subroutine for the fitness function? This is to factor out the matching logic from the routines needed to implement the logic. We could put this into a subroutine that takes a sub-ref for the fitness function and returns the wanted keys/value/output generically.
This can be extended to be more generic by passing in the hash to the fitness function within find_good_keys.
This may seem overkill for the "keys > .4" case, but is lovely for a more complex question like "keys between .3 and .7 inclusive, where the value contains foo (case insensitive)."
| [reply] [d/l] [select] |
by GertMT (Hermit) on Jul 12, 2011 at 06:21 UTC | |
Can someone explain me why it doesn't work as presented here? Thanks,
| [reply] [d/l] |