Franck69 has asked for the wisdom of the Perl Monks concerning the following question:

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],"; } } } #################################################

Replies are listed 'Best First'.
Re: Perl multidimensional arrays with "use strict"
by Joost (Canon) on Jun 15, 2004 at 19:40 UTC
    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 perldata for much more information.
Re: Perl multidimensional arrays with "use strict"
by Aragorn (Curate) on Jun 15, 2004 at 19:47 UTC
      Thx Arjen!

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

      Cya!
      Franck