LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-03-2014, 06:03 AM   #1
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
perl - Array logic


I have this database, where some logging is stored; This is in 'domainnm', 'container', 'item' and 'value';

This data is queried with my perl script and where I keep stumbling is this issue:
Per container there are two entries for 'item' (it's about memory usage, btw - to keep talk easier): one is CurrentUsage, the other MaxUsage (which is the maximum of memory allocated since running).

So I thought to fetch the results in an array. (fetchall_arrayref({} fyi), and then iterate over this array and create a smart hash. Only, I am lacking in the "smart" department today, sorely lacking.

This is what I came up with:

Code:
sub parse_results($)
{
        my $results=shift;

        my %onehash;
        my $itemkey;

        for my $row(@$results)
        {
                for my $field(keys %$row)
                {
                        if($row->{'item'} eq 'CurrentUsage')
                        {
                                $itemkey='current_usage';
                        }
                        elsif($row->{'item'} eq 'MaxUsage')
                        {

                                $itemkey='max_usage';
                        }
                        else
                        {
                                $itemkey=$row->{'item'};
                        }
                        %onehash->{$row->{'container'}} = (
                                $itemkey=>$row->{'value'}
                                );
                }
        }
        print Dumper(\%onehash);
}
This is plainly not doing what I want; I was thinking of a hash (or array, whatever) with fields like this:
container | current_usage | max_usage
Container1 | 12345 | 123456
Container2 | 54321 | 654321
... etc

What am I doing wrong? Perl complains: Using a hash as a reference is deprecated, and Useless use of a constant in void context and Useless use of hash element in void context etc...
 
Old 03-03-2014, 07:11 AM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
I should've lunched sooner... I think this is getting a bit closer (at least), might even be 'the' answer:

Code:
                        %onehash->{$row->{'container'}} = (
                                $itemkey=>$row->{'value'}
                                );
is now rewritten as:
Code:
                        $onehash{$row->{'container'}}->{$itemkey} = $row->{'value'};
This seems to yield the correct results.
 
  


Reply

Tags
perl array hash



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 array to hash fritz001 Programming 3 05-29-2013 03:09 AM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
perl + @INC array Ateo Programming 0 01-22-2007 01:38 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

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
Open Source Consulting | Domain Registration