Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Assign 2D-Arrays to a hash using a key

by aaron_baugher (Curate)
on Feb 20, 2012 at 23:15 UTC ( #955181=note: print w/replies, xml ) Need Help??

in reply to Assign 2D-Arrays to a hash using a key

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', '', 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 => '', version => '12.1(SAG1)'}, '222' => { switch => 'off', ip => '', version => '12.4(FCL5)'}, '333' => { switch => 'null', ip => '', 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 => ['','',''], version => '12.1(5)'},

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

Replies are listed 'Best First'.
Re^2: Assign 2D-Arrays to a hash using a key
by mmartin (Monk) on Feb 21, 2012 at 14:59 UTC
    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?


      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955181]
[Corion]: Last day at $work before 3 weeks of vacation (well, not that I'd be really travelling, but no-work-time ;) )
[robby_dobby]: marto: yeah, I'd eventually visit Glasgow some day - just to visit the home town of GHC :-)
[robby_dobby]: Corion: Oh, that's awesome! Your timing is perfect enough to see all hell break loose when you get back at work :P
[marto]: well, let me know in advance, I'll buy you a pint :)
[Corion]: robby_dobby: No, I'm returning in the second workweek of January. The main hectic parts are in the first days after the start of the new year
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-12-15 10:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.