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

Re^2: Hash table manipulation

by spazm (Monk)
on Jul 11, 2011 at 21:52 UTC ( #913781=note: print w/ replies, xml ) Need Help??


in reply to Re: Hash table manipulation
in thread Hash table manipulation

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


Comment on Re^2: Hash table manipulation
Select or Download Code
Re^3: Hash table manipulation
by GertMT (Friar) on Jul 12, 2011 at 06:21 UTC
    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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913781]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-25 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls