Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

hashing it out.

by penguinfuz (Pilgrim)
on May 10, 2002 at 21:25 UTC ( [id://165780]=perlquestion: print w/replies, xml ) Need Help??

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

Currently I am pushing certain values into an array, but I think my goal would be best accomplished by populating a hash with values from another hash and a `back-tick` command.

The first hash is a Value=>Label which I loop over pushing "Label" into another hash as the key and passing the "Value" to a back-tick command, to be used as the associated keys value. I hope that makes sense. ;)

Here is a snippet of where I am at:
$direction = shift; %iface = ( "if${direction}Octets.2" => 'FastEthernet0/1', "if${direction}Octets.3" => 'FastEthernet0/2', "if${direction}Octets.4" => 'FastEthernet0/3', "if${direction}Octets.5" => 'FastEthernet0/4', "if${direction}Octets.26" => 'GigabitEthernet0/1', ); foreach my $oid (sort keys(%iface)) { %oids = ("$iface{$oid}" => `$snmpwalk $ip $community $oid`); }
The %oids hash seems to be overwriten with each loop, leading me to re-think the process. I was previously pushing the above data onto an array, but I need to keep the label and 'return values' together for later pushing into a db.

I was thinking of another variation on the loop, something like:
foreach my $oid (sort keys(%iface)) { @oids = ({"$iface{$oid}" => `$snmpwalk $ip $community $oid`}); }
However, I am not exactly sure how to get the data pairs back out of the @oids array. Can anyone nudge me in the right direction?

Replies are listed 'Best First'.
Re: hashing it out.
by graff (Chancellor) on May 10, 2002 at 22:19 UTC
    If I understand you right (??), I think you're just using the wrong syntax for the hash assignment inside the foreach. Instead of this:
    foreach my $oid (sort keys(%iface)) { %oids = ("$iface{$oid}" => `$snmpwalk $ip $community $oid`); }
    I think you want this:
    foreach my $oid (sort keys(%iface)) { $oids{$iface{$oid}} = `$snmpwalk $ip $community $oid`; }
    because I think your description said something to the effect of "keys of %iface are used as parameters in a set of back-tick operations, and values of %iface are then used as keys of of %oids -- the values in %oids are the strings returned by the back-ticks." (I'm not asserting that this is any clearer than your own description, but if you get what I mean, then I must have gotten what you meant.)
      > ...I think your description said something to the effect...
      > ...if you get what I mean, then I must have gotten what you meant.

      I think you understand my goal precisely; However, I'm not sure your suggestion will work either. After an initial attempt it did not work but I will continue thinking on it, but for now I am simply pushing the data into an array:
      foreach my $oid (sort keys(%iface)) { push @oids,"$iface{$oid}: ",`$snmpwalk $ip $community $oid`; }
      Not exactly what I want, so I think I will look into Net::SNMP now and revisit the issue when my "hash" skills are a bit stronger. ;)

      Cheers.
Re: hashing it out.
by Anonymous Monk on May 10, 2002 at 22:17 UTC
    It sounds like you might be looking for this:
    %oids = ( ); foreach my $oid ( sort keys %iface ) { $oids{$iface{$oid}} = `$snmpwalk $ip $community $oid`; };
    Does that help?
    -Jules K.
Re: hashing it out.
by lshatzer (Friar) on May 10, 2002 at 22:45 UTC
    Unrelated to your question, but I like to advocate it's use. Use qx as oppsed to `backticks`. To me it is easier to read, and know what is going on, then backticks. At least with certian fonts.
Re: hashing it out.
by dsheroh (Monsignor) on May 11, 2002 at 13:53 UTC
    If I read your question correctly, and you want to store multiple oids for each iface, then you'd probably be best off using a hash of arrays.
Re: hashing it out.
by yodabjorn (Monk) on May 11, 2002 at 10:43 UTC
    Not a direct answer, but instead of using ticks and snmpwalk is this somthing that can be handled with Net::SNMP(CPAN).

    There seems to be a lot of data on PM about using Net::SNMP(Perlmonks)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

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

    No recent polls found