Perl multidimensional arrays with "use strict"
Franck69
created: 2004-06-15 15:01:06
Hi! Little problem there. Can someone tell me what's the problem with this? The first test below works while the second doesn't. I have to use strict so I need to know why it doesn't work and what I have to do to fix the problem in the second test. It complains when I put items in the second dimension of the array..."Can't use string ("group 0") as an ARRAY ref while "strict refs" in use at test.pl line 18." Thx a lot! Franck
#################################################
#FIRST TEST
$i = 0;
$j = 0;
$k = 0;

@data = ();

  for ($i=0; $i<=3; $i++){
    $data[$i] = "group $i";
    
        for ($j=0; $j<=3; $j++){
          $temp1 = $j+$i+10;
          $data[$i][$j] = "user $temp1";
          
            for ($k=0; $k<=3; $k++){
               $temp2 = $k+100;
               $data[$i][$j][$k] = " $temp2";
            }
         }
      }

  for ($i=0; $i<=3; $i++){  
    print "\n\n$data[$i] own: ";  
        for ($j=0; $j<=3; $j++){ 
          print "\n     $data[$i][$j] who have privilege ";     
            for ($k=0; $k<=3; $k++){
print "$data[$i][$j][$k],";
}
}
}
#################################################

#################################################
#SECOND TEST
use strict;
use warnings;


my $i = 0;
my $j = 0;
my $k = 0;

@::data = ();
my $temp1;
my $temp2;

  for ($i=0; $i<=3; $i++){
    $::data[$i] = "group $i";
    
        for ($j=0; $j<=3; $j++){
          $temp1 = $j+$i+10;
          $::data[$i][$j] = "user $temp1";
          
            for ($k=0; $k<=3; $k++){
               $temp2 = $k+100;
               $::data[$i][$j][$k] = " $temp2";
            }
         }
      }

  for ($i=0; $i<=3; $i++){  
    print "\n\n$::data[$i] own: ";  
        for ($j=0; $j<=3; $j++){ 
          print "\n     $::data[$i][$j] who have privilege ";     
            for ($k=0; $k<=3; $k++){
print "$::data[$i][$j][$k],";
}
}
}
#################################################
Re: Perl multidimensional arrays with "use strict"
created: 2004-06-15 15:40:17
You are confusing arrays and hashes, Perl is not PHP ;-)

You can only use integers to index arrays, you can only use strings to index hashes.

You can use (paraphrasing your code)

use strict;
use Data::Dumper;
my %data;
my $name = "group 0";
for my $i (0 .. 2) {
  for my $j (0 .. 3) {
    $data{$name}[$i][$j] = "group 0 - index $i - $j";
  }
}
print Dumper(\%data);

# output follows
__END__


$VAR1 = {
          'group 0' => [
                         [
                           'group 0 - index 0 - 0',
                           'group 0 - index 0 - 1',
                           'group 0 - index 0 - 2',
                           'group 0 - index 0 - 3'
                         ],
                         [
                           'group 0 - index 1 - 0',
                           'group 0 - index 1 - 1',
                           'group 0 - index 1 - 2',
                           'group 0 - index 1 - 3'
                         ],
                         [
                           'group 0 - index 2 - 0',
                           'group 0 - index 2 - 1',
                           'group 0 - index 2 - 2',
                           'group 0 - index 2 - 3'
                         ]
                       ]
        };

See [perldoc://perldata] for much more information.
[id://149675|"What should it profit a man, if he should win a flame war, yet lose his cool?"]
Re: Perl multidimensional arrays with "use strict"
created: 2004-06-15 15:47:09
See perlreftut, perllol and perldsc for a good intro on complex datastructures in Perl (after those, perlref for all the details).

Arjen

Re^2: Perl multidimensional arrays with "use strict"
created: 2004-06-15 16:13:51
Thx Arjen!

By the way, do you know Arjen Anthony Lucassen? ;)

Cya!
Franck

perlmonks.org content © perlmonks.org and Anonymous Monk, Aragorn, Franck69, Joost

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03