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

I receive an error message like this:

"Can't use string ("1") as an ARRAY ref while "strict refs" in use at AllShare_emp_AfrEur.pl line 185."

where line 185 takes the form of:

push @MySeparateArray, $MyArrayOfArrays[$i][$a];

Can anyone suggest what type of problem the error message is getting at? I know I'm not providing much information, but I wouldn't want to paste my 500 line script here. The $i and $a variables should be non-negative integers, and there should definitely be a value at those coordinates in my array-of-arrays. I remember getting a similar error recently while using an array-of-arrays, and ended up just rewriting things a different way - but I'd really like to understand what's going wrong here. I've used AoA's a fair bit, but I don't understand what array reference the error's referring to. Thanks!

Replies are listed 'Best First'.
Re: Array of arrays problem
by ikegami (Patriarch) on Apr 15, 2008 at 23:37 UTC

    $MyArrayOfArrays[$i] holds "1", but $MyArrayOfArrays[$i][$a] expects $MyArrayOfArrays[$i] to have an array reference. In other words, contrary to the variable's name, it's not an array of arrays.

    You should find the following useful:

    use Data::Dumper; print Dumper \@MyArrayOfArrays;

    Just guessing here, but you probably did
    $MyArrayOfArrays[$i] = @some_other_array;
    instead of
    $MyArrayOfArrays[$i] = \@some_other_array;
    at some point.

Re: Array of arrays problem
by TGI (Parson) on Apr 16, 2008 at 00:10 UTC

    The error message means that you are trying to dereference a scalar as that does not contain a reference.

    You are attempting to deref two arrays, $MyArrayOfArrays, and $MyArrayOfArrays[$i]. One of them doesn't have the value you expect it to have. Look at the last place you assigned to these variables. You may also want to try using Data::Dumper to inspect the structure.

    I highly recommend reading and rereading perldsc and perlreftut.

    Update: Since I was so terribly slow posting my advice, I thought I'd add an additional comment in an attempt to make this node useful.

    You got this error because you are using strict. If you weren't using strict, the error message would disappear and your script would run just fine. Except that it wouldn't work. You'd be working with arrays named @1 or @3 or whatever. If you're lucky, you'd wind up with weirdly mangled output and have one hell of a time figuring out what the problem was. Even worse, everything might seem to work just fine...

    I congratulate you on choosing to use strict, and sticking with it, despite getting these error messages. Keep up the good work.


    TGI says moo

Re: Array of arrays problem
by FunkyMonk (Bishop) on Apr 15, 2008 at 23:41 UTC
    You've got something other than an arrayref stored in $MyArrayOfArrays[$i]. Eg
    my @MyArrayOfArrays; my $i = 42; $MyArrayOfArrays[$i] = '1'; $MyArrayOfArrays[$i][1]= 2;

    That gets you a "Can't use string ("1") as an ARRAY ref while "strict refs" in use" error

    Use Data::Dumper to look at @MyArrayOfArrays just before the line in error

Re: Array of arrays problem
by sunwolf78 (Novice) on Apr 16, 2008 at 18:22 UTC
    I figured out the problem...

    My past code reveals that once upon a time, I knew that when building up my array of arrays, I needed to use syntax like:

    push @myArrayOfArrays, [ @myOtherArray ];

    But there seems to have been some corrupted memory in my carbon-based processor, because this time around I would have never guessed about using those square brackets.

      push @myArrayOfArrays, @myOtherArray;
      means
      push @myArrayOfArrays, $myOtherArray[0], $myOtherArray[1], ..., $myOtherArray[n];

      [ @myOtherArray ]
      creates an array, copies @myOtherArray into it, an returns a reference to the new array, so
      push @myArrayOfArrays, [ @myOtherArray ];
      appends an array reference to @myArrayOfArrays instead of simply appending the contents of @myOtherArray.