>
> I have this really annoying problem that I can't seem to figure
> out... I need to create a hash that contains two sets of arrays
> embedded. Editting the array in the hash would look like this:
>
> $db{'key1'}{'key2'}[0][0] = "one_value";
> . . .

that works..

%db would be a hash whose keys map to hashrefs. the key 'key1' would be defined for that hash.

%{ $db{'key1'} } would be a hash whose keys map to array refs. they key 'key2' would be defined for that hash.

@{ $db{'key1'}{'key2'} } would be an array where each element is an array ref.

@{ $db{'key1'}{'key2'}[0] } would be an array whose members are strings. the first element of that array would be "one_value".


to find the contents of the outer hash, you can use:

@outer_keys = keys %db;

and you'll get an array of all keys that point to sub-hashes in the structure.


to find the contents of a sub-hash, you can use:

@inner_keys = keys %{ $db{'key1'} };

and you'll get an array of all keys that point to 2D arrays within that sub-hash.


to find out how many rows are in a particular 2D array, you can use:

$rows = scalar @{ $db{'key1'}{'key2'} };


and to find out how many columns are in a particular row, you can use:

$cols = scalar @{ $db{'key1'}{'key2'}[0] };


> Now, under the key2 key, I don't know how many values are in that
> array.

as mentioned above, this:

$rows = scalar @{ $db{'key1'}{'key2'} };

will tell you.


> Could someone please give me an idea what this hash structure would
> look like, and how I would go about getting the last index number of
> the first set of array numbers. (Something like
> $#{$db{'key1'}{'key2'}} maybe?)

the value $rows, from the code above, will tell you how many items are in @{ $db{'key1'}{'key2'} }, but you need to remember the difference between counting (starts at 1) and indexing (starts at 0) to locate the index of the last item in that list.

in other words, the number you're looking for is $rows-1.


> And also, how would I go about deleting the array? I don't want to do
> delete $db{'key1'}{'key2'}, but something more along the lines of
> delete $db{'key1'}{'key2'}[3].

popping is probably easiest:

pop @{ $db{'key1'}{'key2'} };

mike
.


In reply to Re: Array in a Hash by mstone
in thread Array in a Hash by mt2k

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.