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

There are a number of problems in the snippet of code below, and I fail to see the solution. I have read this node (duplicates in arrays) and this node (duplicates in arrays/hashes), to no avail.

First, I know the initial foreach loop does not work this way. I had solved by putting the while (<input>) on top of it and splitting the lines directly into @line. But what I want is what I try to write below (@data is an array of arrays from a tab delimited file).

Second, the loop breaks because of the if clause that is supposed to catch duplicates. But it looks o.k. to me.

Third, and most important, the three dimensional array of arrays I think I am creating does not materialize in the test. I quite sure I'm making a mistake with the references to arrays but can't come up with the solution.

foreach @line (@data) { for my $tree (0..17) { for my $stem (0..10) { for my $height (0..2) { if (@line[0]==$tree and @line[ +1]==$stem and @line[2]==$height) { if (defined $mdarray[$ +tree][$stem][$height]) { print "there's a dupli +cate stem!\n" } else { $mdarray[$tree][$stem] +[$height]=[@line]; } } } } } } print "Test: @{$mdarray[1][1][1]}\n";
Thank you for your help.

Replies are listed 'Best First'.
Re: Fail to see referencing problem to arrays (in an array of arrays)
by mikfire (Deacon) on Apr 25, 2001 at 03:03 UTC
    Assuming it is not a typo, you have several problems in this loop. I will start at the top and move down.
    foreach @line ( @data )
    I am not sure if this is a typo. What we refer to as "an array of arrays" is really an array of references. In this case, I think you meant:
    foreach $line ( @data )

    This of course will make changes into the 'if' clause, but you have some existing problems in there already.

    if ( @line[0] ...
    does not do what you think. You are referencing an array slice consisting solely of the first element in @line. Perl will DWYM and give you the referenced element of the array. Using -w would have caught this "mistake".

    Again, assuming it isn't a typo, you want to say

    if ( $line[0]...
    in the generic case. In this case, though, since we have changed @line to $line ( ie, from an array to an array ref ), it really should look like
    if ( $$line[0]...
    or ( and this is my preferred form )
    if ( $line->[0] == $tree ... )
    Of course, you will need to substitute this change through the entire line, ie,
    s/\@line/$line->/g;

    Finally, the last assignment probably needs to look like

    $mdarray[$tree][$stem][$height] = $line;
    This will stash the current array reference into the right location for later use.

    All in all, I would highly recommend reading through perldsc ( the perl data structures cookbook ). I found it very useful when I was first learning how to handle perl's data structures. I would also recommend always developing with -w and use strict :)

    mikfire

      Thanks for the explanation and suggestions. As it turns out the root of my confusion lay in accessing the resulting array. Just for completeness sake: I needed to check the existence of a reference using defined before printing.

      for my $tree (0..17) { for my $stem(0..10) { for my $height (0..2) { my $line=$mdarray[$tree][$stem][$heigh +t]; if (defined @$line) {print "$tree,$ste +m,$height : @$line \n" } } } }