It seems to me that you are looking for HoH (hash of hashes) or AoH (array of hashes); anyway, a two level data structure. On the first level, there's the id of this particular device, either the name or a numerical ID, as you hinted at. On the second level, there's a record, a hash, containing all the data for this particular device.

An introduction can be found in perlreftut, which is part of the standard documentation. The complete, authoritive reference is in perlref.

A bit more practical: I would think that there are mainly two ways to add this data to the data structure: either by field, or by record. I'll give an example of both.

my %data; # By record: $data{'Drive C'} = { 'Used Space OID' => '1.2.3.4.5.1', 'Size OID' => '1.2.3.4.6.1', 'Max Size' => 200000, 'Alocation Units' => 4096, }; # By field $data{'Drive C'}{'Used Space OID'} = '1.2.3.4.5.1'; $data{'Drive C'}{'Size OID'} = '1.2.3.4.6.1'; $data{'Drive C'}{'Max Size'} = 200000; $data{'Drive C'}{'Alocation Units'} = 4096;
Now the data structure these produce are identical — provided the hash entry didn't exist yet: $data{'Drive C'} now contains a reference to a hash, which in turn contains all the data for this particular drive. Autovivification will make this secondary hash spring to life in the latter case; for the former, the new anonymous hash replaces whatever was there before.

With sufficient caution, you can use any hash related function on it, for example keys():

foreach(sort keys %{$data{'Drive C'}}) { print "$_ = '$data{'Drive C'}{$_}'\n"; }

Since the value for $data{'Drive C'} is a hash reference, you can copy that value into a scalar — the copy still references to the same hash, you can even modify the original data through it — and access all the data for 'Drive C' in a simpler, possibly even somewhat faster, manner:

my $h = $data{'Drive C'}; foreach(sort keys %$h) { print "$_ = '$h->{$_}'\n"; }

As you hinted towards unique numerical ID's, you might use an array instead, pretty much in the same way:

$data[1] = { 'Used Space OID' => '1.2.3.4.5.1', 'Size OID' => '1.2.3.4.6.1', ... } $data[1]{'Used Space OID'} = '1.2.3.4.5.1';
Well, HTH.

In reply to Re: Array/Hash?? Help Needed by bart
in thread Array/Hash?? Help Needed by LostS

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.