Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Assign 2D-Arrays to a hash using a key by aaron_baugher
in thread Assign 2D-Arrays to a hash using a key by mmartin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found