Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Hash value interpolation

by ikegami (Patriarch)
on Feb 16, 2009 at 20:44 UTC ( [id://744197]=note: print w/replies, xml ) Need Help??


in reply to Hash value interpolation

my $ht = \%h1; while ( my ( $key, $value ) = each(%$ht) ) { ... }

Replies are listed 'Best First'.
Re^2: Hash value interpolation
by csarid (Sexton) on Feb 16, 2009 at 21:13 UTC
    Thanks ikegami, your idea gets me closer however, I get no output. Looks like I'm still doing something incorrect. Below is my actual test script:
    use warnings; use strict; my $COLOR=2; my $hm=""; my $ht=""; my %h1 = ( "1" => "blue" ); my %h2 = ( "2" => "green" ); if ( $COLOR == 1 ) { $ht=\%h1; } elsif ( $COLOR == 2 ) { $ht=\%h2; } sub geth { while ((my $key, my $value) = each(%$ht)) { if ( $key == $COLOR ) { $hm=$value; } } print "$hm\n"; }
      I don't know why you have two hashes in the first place.
      my %colors = ( "1" => "blue", "2" => "green", ); my $COLOR = 2; # 1..2 sub geth { print "$colors{$COLOR}\n"; } geth();

      Or why you aren't using an array.

      my @colors = ( "blue", "green", ); my $COLOR = 1; # 0..1 sub geth { print "$colors[$COLOR]\n"; } geth();
        Thanks for all the help everyone... responding to ikegami's two points, the reason for the two hashes is because I wanted to simplify the code sample to focus on my problem, but the idea was to select a hash table to use based on a calculation. Also, the hash table makes the data elements much more readable for me. Is there a performance gained by using an array over the hash? Thanks gain!

      It appears that geth() is never called. If I fix that, the output is "green".

Re^2: Hash value interpolation
by codeacrobat (Chaplain) on Feb 16, 2009 at 21:22 UTC
    Is it just me or is each an usability fail? I prefer traversing hashes with either keys or values.
    my $ht = \%h1; for my $key ( keys %{$ht} ){ my $val = $ht->{$key}; ... }

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

      The use of each is that it's not a memory hog. When you loop over keys, Perl has somewhere built a list of all the keys in the hash. This is especially disastrous when the hash is tied to some huge data set.

      The real problem with each is that every hash has only one iterator, and if you drop out of a loop that's walking the hash, the next loop that tries to walk that hash will start where the first left off.

        The problem can be avoided by calling keys before each.
        keys %$ht; # Reset iterator while (my ($key, $value) = each(%$ht)) {

        Update: Checked source to make sure it's efficient. Yes it is. It doesn't enumerate the keys at all in void context.


        Kyle wrote "The real problem with each is that every hash has only one iterator, and if you drop out of a loop that's walking the hash, the next loop that tries to walk that hash will start where the first left off."

        Can you give an example where the above mentioned happens or point me to an example.
        Thanks very much

Log In?
Username:
Password:

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

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

    No recent polls found