LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Hashes -- Updating a hash ref via hash value (https://www.linuxquestions.org/questions/programming-9/perl-hashes-updating-a-hash-ref-via-hash-value-948532/)

0.o 06-04-2012 06:24 PM

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!

CTM 06-05-2012 10:56 AM

Looks syntactically valid to me - hash keys are just scalars, so as long as $oids{$name} is a scalar, you should be fine.

pan64 06-05-2012 10:59 AM

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....

sundialsvcs 06-05-2012 12:13 PM

Please send this request over to http://www.perlmonks.org if you haven't done so already.

0.o 06-05-2012 12:16 PM

Quote:

Originally Posted by pan64 (Post 4696083)
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.

pan64 06-05-2012 12:45 PM

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.


All times are GMT -5. The time now is 07:10 AM.