ybiC has asked for the wisdom of the Perl Monks concerning the following question:

In the following chunk, how can I have the print "\n == hash key == $oid{$value} ==\n"; line print the hash key as well as the value?
    thanks in advance,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")
#! /usr/bin/perl require 5.6.1; # for Net::SNMP v4 use strict; # avoid D'oh! prob +lems use warnings; # avoid D'oh! prob +lems use Net::SNMP 4 qw(snmp_dispatcher oid_lex_sort); # must be >= versi +on 4 use Tie::IxHash; # insertion-order +retrieval my ($session, $error) = Net::SNMP->session( -hostname => 'catalyst', -community => 'public', ); unless (defined($session)) { print "Error: $error"; exit; } tie my %oid, "Tie::IxHash"; %oid = ( TpFdbAddress => '.1.3.6.1.2.1.17.4.3.1.1', TpFdbPort => '.1.3.6.1.2.1.17.4.3.1.2', bridgePortNum => '.1.3.6.1.2.1.17.1.4.1.1', switchPortNum => '.1.3.6.1.2.1.17.1.4.1.2', ifDescr => '.1.3.6.1.2.1.2.2.1.1', ifName => '.1.3.6.1.2.1.31.1.1.1.1', ); for my $value (keys %oid) { if (defined(my $response = $session -> get_table(-baseoid => $oid{$v +alue}))) { print "\n == hash key == $oid{$value} ==\n"; for my $r(oid_lex_sort(keys(%{$response}))) { print($response -> {$r}, ",\n"); } } else { print "Error: ",($session -> error(), ",\n"); } } $session->close; exit;

Replies are listed 'Best First'.
Re: Print hash value and key in for keys loop
by danboo (Beadle) on Dec 14, 2001 at 02:25 UTC
    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
      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

Re: Print hash value and key in for keys loop
by Zaxo (Archbishop) on Dec 14, 2001 at 02:42 UTC
    print "\n == $value == $oid{$value} ==\n";

    After Compline,
    Zaxo