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

I'm working with an array of arrays of arrays. I chose this method to deal with information that is variable in occurrence in three aspects. My main array is called @MAIN. Inside of MAIN is seq_unique, inside of which is seq_position. This is such as too accomodate a sequence (biological data) that could occur numerous times in different manners within a single file. I have found that if I try to use
print FILEHANDLE $MAIN[$seq_unique][$seq_position]\n";

that it returns
SCALAR(0x126ac8)<BR>
in the file addressed by FILEHANDLE
I'm expecting it to output the contents of that array there. What's wrong? The preceeding command to that is
$MAIN[$seq_unique][$seq_position] = \(@array[1 .. 21]);

where @array has data from a line of an input file with information corresponding to the occurrence called $seq_position of the sequence called $seq_unique.
thanks!

Replies are listed 'Best First'.
Re: Array output as SCALAR
by sauoq (Abbot) on Jul 07, 2003 at 23:27 UTC
    $MAIN[$seq_unique][$seq_position] = \(@array[1 .. 21]);

    It seems you are making a couple mistakes with that.

    First, \(@array[1 .. 21]) returns a list of references to the elements in that array slice. I.e. it is the same as:

    ( \$array[1], \$array[2], . . ., \$array[21] );
    That's probably not what you want.

    Your other mistake is that you are assigning a list to a scalar. The result will be that the scalar will equal the last item in the list. So, you code is really the same as:

    $MAIN[$seq_unique][$seq_position] = \$array[21];
    So, it's a reference to a scalar, and that's why, when you print it, you get that SCALAR(0x236ac8).

    I'm not sure how to tell you to fix it because it isn't obvious what you want. Do you want that to hold a reference to an array, which is what it looks like you were trying to achieve with \(@array[1 .. 21])? Or do you want it to hold a readable string, which is what it seems you want if you are trying to print it out?

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Array output as SCALAR
by Zaxo (Archbishop) on Jul 08, 2003 at 00:01 UTC

    sauoq gives an excellent diagnosis. The syntax for a 3-d array is,

    $MAIN[$seq_unique][$seq_position] = [@array[1 .. 21]]; print "@{$MAIN[$seq_unique][$seq_position]}";
    The array in the print statement is quoted to give the effect of separating the printed elements by $".

    After Compline,
    Zaxo

      Thanks! Thats just what I needed to get it to do...
Re: Array output as SCALAR
by aquarium (Curate) on Jul 07, 2003 at 23:31 UTC
    you are assigning a reference to an anonymous slice. If you really want to stuff the 1..21 elements of the @array into the scalar $MAIN[$seq_unique][$seq_position] then the RHS should be join / / @array[1..21];
    Not sure exactly what you're trying to do..please explain.