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

my @array = (["w", "h", [1, 2], "c"]);
How do I access the second array? I have tried the following and cannot get an output.
my @array = (["w", "h", [1, 2], "c"]); print $array[0]->[0]; print $array[0]->[1]; print $array[0]->[2];

The output I get is whARRAY(0x1770fa), I cannot access the second array. What am I doing wrong?

Replies are listed 'Best First'.
Re: Multi dimensional arrays
by Mr. Muskrat (Canon) on Feb 07, 2003 at 22:32 UTC

    You need to access the proper element and dereference it.

    use strict; use warnings; my @array = ("w", "h", [1, 2, "c"]); print $array[0],$/; # w print $array[1],$/; # h print $array[2],$/; # ARRAY(0x177f07c) You've found the array referenc +e! print ${$array[2]}[0],$/; # 1 print ${$array[2]}[1],$/; # 2 print ${$array[2]}[2],$/; # c

    See tye's References quick reference for more details!

      You need to access the proper element and dereference it.
      Yes, but why in such a difficult way?
      print ${$array[2]}[0],$/; # 1
      can be written as print $array[2]->[0], $/; where you can then even leave out the '->' as it stands between two subscripts, leading to print $array[2][0], $/;

      -- Hofmator

        Just because it can be written that way doesn't mean that we should teach it that way. It is much better for someone to learn how it works before they learn the shortcuts.
Re: Multi dimensional arrays
by simon.proctor (Vicar) on Feb 07, 2003 at 22:30 UTC
    Firstly my snippet:
    use strict; use warnings; my @array = ("w", "h", [1, 2, "c"]); foreach (@array) { if(ref($_) eq 'ARRAY') { foreach (@{$_}) { print $_,"\t"; } print "\n"; } else { print $_,"\n"; } }
    Now what I am doing is iterating over the contents of the array and testing each element using the builtin 'ref'. Now if the array element is an arrayref, I iterate over that and print the contents. This prints:
    w h 1 2 c
    To print the contents of the array ref - I first have to dereference it by using @{}. This is the way I do it though there are most likely other methods.

    I suggest you look at Advanced Perl Programming for more details :).

    HTH
    SP

    PS: please read Writeup Formatting Tips when you post!
Re: Multi dimensional arrays
by cees (Curate) on Feb 08, 2003 at 06:23 UTC
    my @array = (["w", "h", [1, 2], "c"]);

    First of all you should realize that the above code creates a one element array that contains a reference to a 4 element array. The square brackets indicate that you are creating or returning a reference to an array so you end up with a three dimensional array. You probably intended to use ("w", "h", [1, 2], "c") which would give a two dimensional array. Your code is perfectly legal, but probably not what you expected.

    So with your original code $array[0] will contain a reference to a four element array. To look at the first element in this array, you have properly dereferenced it by using the -> operator and that is why $array[0]->[0] returns "w".

    Now $array[0]->[2] contains a reference to your embedded array, so to access the the elements of this array, you must dereference again, by using the -> operator again. So $array[0]->[2]->[0] contains the number 1 and $array[0]->[2]->[1] contains the number 2.

    The reason you are seeing ARRAY(0x1770fa) when you print $array[0]->[2] is because that contains the reference to the array [1, 2]. The value that a reference contains is the memory location of the actual array that it points to, and that is what is printed out.

    Just remember that [] always returns an array reference and needs to be dereferenced to access it.