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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.