Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Assign 2D-Arrays to a hash using a key

by mmartin (Monk)
on Feb 20, 2012 at 21:50 UTC ( #955162=perlquestion: print w/ replies, xml ) Need Help??
mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm trying to assign the values from a couple of 2-D Arrays into a hash based on a "key" within the array.
I haven't messed with hashes in a while so I'm a little rusty. I'm having trouble figuring out how to assign the values, I looked for an example online but alas Google has failed me lol...

What I have is 5 separate 2-Dimensional Arrays. All arrays have the same setup.
For Example: @myArray[key][value]
### I'm using 5 separate arrays but for the sake of space I'll just do + 3 here... $first[0][0] = "111" $first[0][1] = "on" $first[1][0] = "222" $first[1][1] = "off" $first[2][0] = "333" $first[2][1] = "null" ...etc... ############################ $second[0][0] = "111" $second[0][1] = "10.1.1.1" $second[1][0] = "222" $second[1][1] = "12.1.1.1" $second[2][0] = "333" $second[2][1] = "13.1.1.1" ...etc... ############################ $third[0][0] = "111" $third[0][1] = "12.1(SAG1)" $third[1][0] = "222" $third[1][1] = "12.4(FCL5)" $third[2][0] = "333" $third[2][1] = "12.1(5)" ...etc... ############################ ### Then have a hash with all the values combined. # i.e. the hash should look something like this after a 'Data::Dump': #--------- %all_data ---------# VAR1 = '111' => "on", => "10.1.1.1", => "12.1(SAG1)"; VAR2 = '222' => "off", => "12.1.1.1", => "12.4(FCL5)"; VAR3 = '333' => "null", => "13.1.1.1", => "12.1(5)";

I'm having trouble figuring out if I need a loop or what not, because not all the arrays are the same length.
Occasionally a couple of the arrays might have more than one value for a particular key...
i.e. It might have 2 values like this below for the key "111".
$fourth[111][125]
$fourth[111][200]


If anyone has any hints that could point me in the right direction that would be great...
I've read the PerlDocs on hashes but I am still a little confused.


Thanks in Advance,
Matt

Comment on Assign 2D-Arrays to a hash using a key
Download Code
Re: Assign 2D-Arrays to a hash using a key
by aaron_baugher (Deacon) on Feb 20, 2012 at 23:15 UTC

    There are a couple ways you could go with this. One would be to create a hash of arrays. So the hash key '111' would point to an array containing 'on', '10.1.1.1', and '12.1(SAG1)', for example.

    However, since you said some keys may have multiple values for some positions, that may be too restrictive. So another method would be to create a hash of hashes, keying the secondary hashes with whatever keys fit your data. For example:

    my %hoh = ( '111' => { switch => 'on', ip => '10.1.1.1', version => '12.1(SAG1)'}, '222' => { switch => 'off', ip => '12.1.1.1', version => '12.4(FCL5)'}, '333' => { switch => 'null', ip => '13.1.1.1', version => '12.1(5)'}, ); # $hoh{333}{switch} = 'null';

    By doing it this way, it doesn't matter if a key doesn't have a 'switch' value, for instance; it won't throw off the rest of the array. And if a particular sub-key may have multiple values, you can give it an array of its own, like so:

    '333' => { switch => 'null', ip => ['13.1.1.1','13.2.2.2','13.3.3.3'], version => '12.1(5)'},

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      Hey aaron_baugher,

      Thanks for the reply..!

      Now that I think about, I guess I kinda gave a bad example. For the values that might be duplicated, they would be integers which "should" be added together and then stored into the hash. So I guess there wouldn't be a value that has more than one value for particular record.

      With this new info do you still think it's necessary to do a hash of hashes still?

      Thanks,
      Matt


        It may not be necessary, if your data is reliable enough that you know there will always be the same number of values in the same order for each key. But I still like the hash-of-hashes because it tends to be easier to remember key names than index numbers. For instance, which of these makes it easier to recognize what it contains?

        $hash{key}{ip_address}; # or $hash{key}[1];

        On the other hand, arrays are faster and use less memory than hashes, so if you're pressed for speed or resources, a hash-of-arrays may be the better solution.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Assign 2D-Arrays to a hash using a key
by Anonymous Monk on Feb 21, 2012 at 00:44 UTC
    The magic is called "references." Go read or Google the perldocs on that concept. The value that gets stored in an array, a list, or a hash can be a scalar value of various kinds, or... it can be a reference, which therefore can refer to anything at all.
Re: Assign 2D-Arrays to a hash using a key
by mmartin (Monk) on Feb 21, 2012 at 16:59 UTC
    Hey guys thanks for your help,

    Ok so the thing with the array that it might have more then one value for a particular key, I was able to work around. Since the "values" that would have multiples were integers I just simply added them together then re-assigned the "temporary" array back to the original empty array. So that part is fixed...


    So for assigning to a hash I was able to find some older scripts I wrote that had hashes and was able to use that and figure it out.

    So since there were 5 different 2D-Arrays I came up with this to build the hash:
    ###These were the names of the arrays my (@AP_name, @AP_ipAddress, @AP_IOSversion, @AP_channelUtil, @AP_numC +lients); ################################################################### for (my $x = 0; $x <= $#AP_name; $x++) { $AP_data{ "$AP_name[$x][0]" } = { NAME => "$AP_name[$x][1]", IPADDRESS => "$AP_ipAddress[$x][1]", VERSION => "$AP_IOSversion[$x][1]", UTILIZATION => "$AP_channelUtil[$x][1]", NUMCLIENTS => "$AP_numClients[$x][1]" }; }

    And since the arrays are already presorted when I collect the data it made it much easier to build the hash and not have to worry about comparing the Key/identifiers...


    Thanks for your help..!

    Thanks,
    Matt


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-12-28 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls