in reply to Print hash value and key in for keys loop

I think you may be confused about which is the the key and which is the value. For instance; you say "for my $value (keys %oid) {" as though you expect to get a list of values when your actually getting keys. And in your print statement you have "hash key == $oid{$value}" which is printing the value and not the key.

A hash is key/value pairs. You use the keys to lookup the corresponding values. Perhaps what you want is:

for my $key (keys %oid) { if (defined(my $response = $session -> get_table(-baseoid => $oid{$k +ey}))) { print "\n == hash key == $key == hash value == $oid{$key} ==\n"; for my $r(oid_lex_sort(keys(%{$response}))) { print($response -> {$r}, ",\n"); } } else { print "Error: ",($session -> error(), ",\n"); } }
- danboo

Replies are listed 'Best First'.
Re: (2) Print hash value and key in for keys loop
by ybiC (Prior) on Dec 14, 2001 at 02:48 UTC
    Thanks for the quick answer, brother danboo - that does the trick.   8^)

    Actually, I do understand which is key and which is value (at least in the declaration chunk).   What's cornfusing here is that $oid{key} refers to the key, while $key refers to the value.   So to avoid that puzzlement, here's what I'm doing for now:

    for (keys %oid) { if (defined(my $response = $session -> get_table(-baseoid => $oid{$_ +}))) { print "\n == $_ == $oid{$_} ==\n"; for my $r(oid_lex_sort(keys(%{$response}))) { print($response -> {$r}, ",\n"); } } else { print "Error: ",($session -> error(), ",\n"); } }

        cheers,
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")

    Update:
    Hrm... as good monk danboo points out below, I mispoke.   What I meant to say is "What's cornfusing here is that $oid{key} refers to the value, while $key refers to the key."   I really do understand key and value...   really...   honest   {grin}

      My pleasure to assist, though I think we're still not in agreement on our terms. $oid{$key} is not the key; $key is. $oid{$key} is the value. By using the key in the hash %oid we get back the value in return.

      Hopefully I'm not confusing the situation.

      - danboo