Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of associative arrays 1

Status
Not open for further replies.

msc0tt

IS-IT--Management
Jun 25, 2002
281
CA
Hi,
I can't Goggle any examples of an 'array of associative arrays'. Referencing an element isn't too hard - for example:
x = $cars{$color}[$year]

My problem is trying to recurse the entire structure:
for ($yr = 1960; $yr <= 2003; $yr++) {
foreach $color (keys(%cars[$yr])) {
x = $cars{$color}[$yr]
}
}

The argument to my kes() function is what's choking. I tried %cars{}[$yr] which also failed. Basically, I need the syntax to specify an entire asso. array that is within a larger array (non asso.).

-with thanks
Mike

My error is:
Can't use subscript on hash deref at ./userDetailed.pl line 73, near &quot;$hr]&quot;
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not array element) at ./userDetailed.pl line 73, near &quot;])&quot;
 
Mike,
The main problem is you are trying to access a hash with the array element notation. What you are looking for is something like:
Code:
for ($yr = 1960; $yr <= 2003; $yr++) {
  foreach $color (keys(%cars)) {
   $x = $cars{$color}[$yr];
  }
}
This should solve your problem as written. I do wonder if your data structure is designed the way you intend. Is the color really the primary key, or is the year? Your's implies that color is the main factor, but you are iterating with year in your outer loop. Just curious as this can be fairly inefficeint if you have sparsely populated data structutres. Just my oppinion.

Hope that helps.
 
Thanks for the reply.
My example was ambiguous. In your example, you use &quot;keys(%cars)&quot; which I interpret as only one asso. array. To clarify, my structure needs to be one non-associative array of several associative arrays. So, refering to a single element would be: $cars[$year]{$color} right? (or is it $cars{$color}[$year] ???).

To help illuminate, my structure might contain:
%cars[1995] = {&quot;blue&quot;, 3, &quot;black&quot;, 6, &quot;white&quot;, 1};
%cars[1999] = {&quot;black&quot;, 4};
%cars[2002] = {&quot;red&quot;, 2, &quot;white&quot;, 4};

For the sake of argument, the integer values of each element could be the number I own (yeah, right! ;-{) Now, how would I use the keys() function to determine all the colors (&quot;red&quot; and &quot;white&quot;) of cars in 2002? %cars[2002]?

I hope this is more clear.
 
OK, if you want a single array, with the elements being anonymous associative arrays/hashes your first reference would be correct ( $cars[$year]{$color} ). To use keys you'd do something like:
Code:
@colors = keys( %{$cars[$year]} );
Basically %{$cars[$year]} says take the refrence in $cars[$year] and dereference it as a hash.

Here is a quick example based on your clarifications.
[/code]
$cars[1995] = {&quot;blue&quot;, 3, &quot;black&quot;, 6, &quot;white&quot;, 1};
$cars[1999] = {&quot;black&quot;, 4};
$cars[2002] = {&quot;red&quot;, 2, &quot;white&quot;, 4};
for $year (1960..2003) {
if (defined $cars[$year]) {
while ( ($color, $number) = each %{$cars[$year]} ) {
print &quot;You have $number $color $year\n&quot;;
}
} else {
print &quot;You have no $year\n&quot;;
}
}
[/code]
Hope that helps.
 
Yes, this TOTALLY helps. %{$cars[$year]} -- Who woulda thunk it?!?
Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top