Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Hash of arrays of hashes

by NetWallah (Canon)
on Apr 14, 2013 at 18:40 UTC ( [id://1028638]=note: print w/replies, xml ) Need Help??


in reply to Hash of arrays of hashes

Change your data structure to be closer to how your data actually looks.
I.E. instead of squeezing a hashref into an array, make it a real hashref:
Replace "[]" with "{}" in your data;
You can always "SORT" it later, if necessary.
my %INPUT_DATA = ( 'DEVICE1_NAME' => { '1/1/5/40' => { 'ACTUAL' => { 'SVC' => '239', 'SPTM' => '112', }, 'NEW' => { 'SVC' => '239', 'SPTM' => '183', }, }, }, ... # Now, you can use: #....set $device_name #.... foreach my $current_port ( sort keys %{$INPUT_DATA{$device_name}} ) { my @CURRENT_PORT_STATUS = print( "show xdsl operational\-data line + $current_port detail"); }
A simple sort is used above. In real life, your sort sub may be complicated if you want to preserve true numeric order in the port.
One alternative is to add an ORDER field within the port info, and manually increment it. That field can subsequently be used as a sort key. There are also modules that provide (tied) ordered-key hashes. (eg: Data::XHash).

Update: If you really wanted to keep the current data structure (in case it came from outside your control), you will need to process every other element : i.e. skip the hashref, process only the "key" which is a scalar.
To do this, the first thing in the "for loop" should be:

next unless ref $current_port eq ""; # skip if $current port is NO +T a scalar.

             "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
        -- Dr. Cox, Scrubs

Replies are listed 'Best First'.
Re^2: Hash of arrays of hashes
by AnomalousMonk (Archbishop) on Apr 14, 2013 at 20:24 UTC
    next unless ref $current_port eq ""; # skip if $current port is NOT a +scalar.

    Wouldn't this be better if more directly stated as
        next if ref $current_port;  # skip if $current port IS a reference
    thus avoiding the double negative?

      Agreed that your code is shorter/better. Disagree that mine has a double-negative.

                   "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
              -- Dr. Cox, Scrubs

        Disagree that mine has a double-negative.

        Perhaps a matter of interpretation here. What I was thinking was that  unless can be re-written as  if not and  ref $current_port eq "" as  not ref $current_port yielding
            next if not not ref $current_port;

        Perhaps I should have written "implicit double negative". :)

Re^2: Hash of arrays of hashes
by bArriaM (Novice) on Apr 15, 2013 at 13:19 UTC

    Thanks NetWallah, my logic was good but my idea wasn't. I thought that I needed that ARRAY of ports but when I saw your example I understood that it wasn't required at all. Much more efficient the way you showed me! In fact I was able to "fix" my problem by creating 2 independent HASH. But I tried your way and it does the right job. Thanks again, I appreciate it!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found