Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

my @hash = ( .1 => 'url1', .2 => 'url2', .3 => 'url3', .7 => 'url7', .9 => 'url9', );

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:

#define your data: my @hash = ( .1 => 'url1', .2 => 'url2', .3 => 'url3', .7 => 'url7', .9 => 'url9', ); #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"; } ## grab just the urls from the good keys, without including the keys. my @good_values = @hash{ @good_keys }; ##e.g. save a hash with just the subset of new keys my %good_hash; @good_hash{ @good_keys } = @hash{ @good_keys };

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.

my %hash = ( .1 => 'url1', .2 => 'url2', .5 => 'url5', .9 =>'url9' ); my $fitness = sub { $_ > .4 }; my @good_keys = find_good_keys( \%my_hash, $fitness ); #... sub find_good_keys { my ($hashr, $fitness) = @_; return map { $fitness->($_) keys %$hashr }

This can be extended to be more generic by passing in the hash to the fitness function within find_good_keys.

sub find_good_keys { my ($hashr, $fitness) = @_; return map { $fitness->($_, $hashr ) } keys %$hashr }

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)."

my $fitness = sub { my ($key, $hashr ) = @_ ; return .3 <= $key && . 7 >= $key && $hashr->{$key} =~ m/foo/i ? 1 : +0 } my @good_keys= find_good_keys( \%hash, $fitness); sub find_good_keys { my ( $hashr, $fitness ) = @_; return map { $fitness->( $key, $hashr ) } keys %$hashr; }

In reply to Re^2: Hash table manipulation by spazm
in thread Hash table manipulation by sarvan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found