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

Hi Monks - I have been trying to do this (tested and working as intended, gives "-->a,4,6<--" out) :
use strict; my(@Array1); my(@Array2); @Array1 = (0,1,'a',3,4,5,6); @Array2 = (2,4,6); @Array1 = @Array1[ ( @Array2 ) ]; printf STDOUT ("-->%s<--\n", join( ",", @Array1 ) );
But with array references. My line looks like this :
@{ $Row->{ Cells } } = @{ $Row->{ Cells }[ ( @$FieldOrder ) ] };
But I'm getting 'Not an ARRAY reference' errors. Thanks in advance, and apologies for what is probably a basic question.

Replies are listed 'Best First'.
Re: Array refs problem ...
by ikegami (Patriarch) on Jun 24, 2009 at 16:20 UTC
    Find out which array dereference isn't getting the array reference it expects.
    • $Row->{ Cells }?
    • $FieldOrder?
    • $Row->{ Cells }[ ( @$FieldOrder ) ]?

    Update: Your use of 0+@$FieldOrder is very suspicious.

    Update: You have an array slice in the top code (note the "@" in @Array1[ LIST ]), but you have an array lookup in the bottom code (note the "$" in $Row->{ Cells }[ SCALAR ])

    The equivalent of

    @Array1[ LIST ] ^ |
    is
    @{ $array_ref }[ LIST ] ^ |

    You want

    @{ $Row->{ Cells } } = @{ $Row->{ Cells } }[ @$FieldOrder ];

    See Dereferencing Syntax and References Quick Reference.

      Yeah I think the problem is with the way I'm dong that somehow. Adding :
      printf STDOUT ("1-->%s<--\n", $Row->{ Cells } ); printf STDOUT ("2-->%s<--\n", $FieldOrder ); printf STDOUT ("3-->%s<--\n", $Row->{ Cells }[ ( @$FieldOrder ) ] ) ;
      Gave me
      1-->ARRAY(0x2af07dc)<-- 2-->ARRAY(0x2ab07dc)<-- 3-->HASH(0x2b50058)<--
      I don't know what I would have expected for the last one (I think that's like saying $Array[1..4], which surely should be written @Array[1..4], but what do I know ...)
      Update Thanks, but tried that first, didn't work, same error.
      Update2 Oh wait, I missed that you removed the ( ) around @$FieldOrder also, I seem to be making progress now. Many thanks!

        Oh wait, I missed that you removed the ( ) around @$FieldOrder also, I seem to be making progress now. Many thanks!

        Only because they were distracting. They have absolutely no effect here.

        Just like you wouldn't use $z=($x)+($y) instead of $z=$x+$y, there's no reason to use @a[(@$b)] instead of @a[@$b].

Re: Array refs problem ...
by NetWallah (Canon) on Jun 24, 2009 at 19:58 UTC
    I think what you are trying to do is:
    $Row->{ Cells } =[ @{ $Row->{ Cells } }[ @$FieldOrder ] ];
    The @{ $Row->{ Cells } } is the array , which you slice using [ @$FieldOrder ];
    Then generate an anonymous array ref (The outermost [ ] )
    The anon array ref is assigned to the scalar $Row->{ Cells } ,

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Array refs problem ...
by mscharrer (Hermit) on Jun 24, 2009 at 16:47 UTC
    I agree with ikegami's post above.

    You should try to avoid to surround the arrays with ( )'s. This is (IMHO) never necessary and just adds another pair to be tracked by the programmer/maintainer/whoever read the source code. Also the double line spacing used here makes the code a little less readable.