Associative arrays and multi-dimensional arrays. 
Author Message
 Associative arrays and multi-dimensional arrays.

Hi all,

I have a little understanding problem, and total new on writing programs
in Perl. So excuse me, if I hit the bottom.

Here comes my question. How do I traverse an multi-dimensional array.

I have put in information this way :

   $var1{$no}{$i} = $first;

moreover, I not sure this is the way to do it. Perl dosen't cry on the
syntax. But if it is, how do get my data again?

I have tried something like this :

foreach $key1 (sort keys (%var1)) {
    foreach $key2 (keys %{$var1{$no}}) {
         $perhaps = $var1{$key1}{$key2};  # Here I go totally wrong

but then I stopped, because I don't know how to address multi-assigned
values.

Thanks in beforehand.

Best regards




Sun, 19 Jul 1998 03:00:00 GMT  
 Associative arrays and multi-dimensional arrays.
:    $var1{$no}{$i} = $first;
: moreover, I not sure this is the way to do it. Perl dosen't cry on the
: syntax. But if it is, how do get my data again?
This is fine.

: I have tried something like this :
: foreach $key1 (sort keys (%var1)) {
       this looks good, you don't need the sort unless you want
:     foreach $key2 (keys %{$var1{$no}}) {
                                  ^^^ should be $key1
:          $perhaps = $var1{$key1}{$key2};  # Here I go totally wrong
           now you are fine
: but then I stopped, because I don't know how to address multi-assigned
You are RIGHT there .. just play around!

Check out perldsc and perllol in the latest documentation.

Here is a working example:

# initialize test hash
%hash = ( 'k1' => {'lev21k1' => 'value11', 'lev21k2' => 'value12'},
          'k2' => {'lev22k1' => 'value21', 'lev22k2' => 'value22'}
        );

# print out the values of the hash
foreach $key1 (keys %hash) {
  foreach $key2 (keys %{$hash{$key1}}) {
    print '$hash{', $key1, '}{', $key2, '} = ', $hash{$key1}{$key2}, "\n";
  }

Quote:
}

This outputs:

$hash{k1}{lev21k1} = value11
$hash{k1}{lev21k2} = value12
$hash{k2}{lev22k2} = value22
$hash{k2}{lev22k1} = value21  

hope this helps
MRK



Mon, 20 Jul 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Extracting an array out of a multi-dimensional associative array

2. Perl4: Associative multi-dimensional arrays?

3. Multi-dimensional Associative Arrays

4. Perl4: Associative multi-dimensional arrays?

5. Associative array of a two dimensional array

6. arrays, multi-dimensional arrays, pointers and such

7. Multi-Dimensional arrays, DBMs, and Assoc. Arrays

8. dynamically naming arrays in a multidimensional associative array

9. 2-dimensional array into 1 dimensional array?

10. associative array or associative arrays

11. Associative array of associative arrays

12. How to manipulate 2 dimensional Associative arrays?

 

 
Powered by phpBB® Forum Software