LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-04-2012, 07:24 PM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Perl Hashes -- Updating a hash ref via hash value


Ok. So I am trying to update a hash ref that's returned by a net::snmp call using the value of another hash as the key for the reference. Is this possible? the relevant part of the code is below:

Code:
                                elsif($name =~ /ifInOctects/i)
                                {
                                        if($prevresult->{$oids{$name}} > $result->{$oids{$name}})
                                        {
                                                my $prevalue = $prevresult->{$oids{$name}};
                                                my $curvalue = $result->{$oids{$name}};
                                                if($prevalue > $curvalue)
                                                {
                                                        my $totval=$curvalue+$prevalue;
                                                        $result->{$oids{$name}}=$totval;
                                                }
                                        }
                                }
I am trying to maintain state if a counter rolls. Below this I am storing the data in a flat file via Storable. Is this code valid?


Thanks!
 
Old 06-05-2012, 11:56 AM   #2
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware64 14.0 / 14.1
Posts: 131

Rep: Reputation: 50
Looks syntactically valid to me - hash keys are just scalars, so as long as $oids{$name} is a scalar, you should be fine.
 
Old 06-05-2012, 11:59 AM   #3
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,169

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
I do not really understand your problem, but:
Code:
                               elsif($name =~ /ifInOctects/i)
                                {
                                        if($prevresult->{$oids{$name}} > $result->{$oids{$name}})
                                        {
                                                my $prevalue = $prevresult->{$oids{$name}};
                                                my $curvalue = $result->{$oids{$name}};
                                                if($prevalue > $curvalue)                       # << this if is always true, because it is the same as the previous one
                                                {
                                                        my $totval=$curvalue+$prevalue;
                                                        $result->{$oids{$name}}=$totval;
                                                }
                                        }
                                }
otherwise really hard to say anything. In your first line enter #!/usr/bin/perl -w, and also add use strict. This will help you make better code....
 
Old 06-05-2012, 01:13 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
Please send this request over to http://www.perlmonks.org if you haven't done so already.
 
Old 06-05-2012, 01:16 PM   #5
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by pan64 View Post
I do not really understand your problem, but:
Code:
                               elsif($name =~ /ifInOctects/i)
                                {
                                        if($prevresult->{$oids{$name}} > $result->{$oids{$name}})
                                        {
                                                my $prevalue = $prevresult->{$oids{$name}};
                                                my $curvalue = $result->{$oids{$name}};
                                                if($prevalue > $curvalue)                       # << this if is always true, because it is the same as the previous one
                                                {
                                                        my $totval=$curvalue+$prevalue;
                                                        $result->{$oids{$name}}=$totval;
                                                }
                                        }
                                }
otherwise really hard to say anything. In your first line enter #!/usr/bin/perl -w, and also add use strict. This will help you make better code....
This is just a small part of the code. I am using warnings and also strict; they are defined at the top of the code. The two values you mention aren't the same. The $prevresult variable is a hash reference to the previous $result hash reference (make sense?). I am using the module 'Storable' to store the result hash into a flat file and reading it back in on the next run (into $prevresult). I do this to be able to keep-state between itterations of the script for various bits of data.
 
Old 06-05-2012, 01:45 PM   #6
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,169

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
I have just read the page of storable. What I see is you can store and retrieve the hash, but otherwise the hash is not tied to the file (it means the perl tie is not in use, the hashes you use are just working as hashes and nothing else in the code you gave).
So we do not know what kind of (ref, integer, array or ??) value stored in $prevresult->{$oids{$name}} or $result->{$oids{$name}}. Also we do not know which hash is storable: oids or result/prevresult (yes, I see probably the result itself)

In the third line you compare them, in the fifth and sixth line you store them in new variables and in the seventh line you compare these copied values (again).
With other words: I see you compare the same $prevalue and $curvalue in line 3 and line 7. From my point of view lines 5-11 altogether can be written as: $result->{$oids{$name}} += $prefresult->{$oids{$name}}, but probably I missed some info.

So I cannot say more: if perl accepts it you can use it, but remember the code never will do what you wish but what you implemented.




__________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.

Last edited by pan64; 06-05-2012 at 01:46 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Hash of Hash reference query kdelover Programming 1 02-19-2011 05:47 AM
Need help to sort hash of hashes by value in perl newbee21 Programming 2 12-21-2010 08:03 PM
[SOLVED] Perl - How to store and access an array in a hash of hashes? DevonB Programming 4 07-07-2010 08:57 AM
Perl hash of hashes props666999 Programming 2 09-07-2006 05:43 AM
Passing hash of hashes as a parameter (Perl) rose_bud4201 Programming 8 04-21-2005 08:18 PM


All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration