in reply to Array in a Hash
>
> 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
.
|
|---|