in reply to Re: Array of arrays
in thread Array of arrays

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^3: Array of arrays
by jdporter (Paladin) on Feb 28, 2006 at 18:46 UTC
    Well, this part I think you know:
    my @row; while ( @row = $sth_C->fetchrow_array ) { undef @row; }
    So that leaves what's happening with each @row inside the loop.
    my $krow = join "\0", @row;
    Here (above), I'm constructing a single string to represent the contents of the entire row. It's fairly expensive, in the sense that the contents of the entire row are duplicated. So if the row contains a kilobyte of data, $krow will too.

    This string is going to be used as a key in a hash. In fact, that's why I had to make a string: so I could index a hash with the data in an array.
    $seen{$krow}++ and next;
    Here, I'm attempting to access an element of the hash using the key string I generated. The value of such an element is going to be interpreted as a number, because ++ only works on numbers. (Actually that's not quite true; but the exception doesn't concern us now.)

    If no such element is found, it is created first, and the "treat as number" effect of ++ initializes the element to 0.

    Next, the current value is tested for truthiness, and if is true, the remainder of the loop is skipped via next. This means that the remainder of the loop will be executed only the first time this hash element is seen.

    Lastly, the hash element is incremented. This will keep the remainder of the loop from being executed for this hash key ever again.
    print OUTPUT_FILE join( "\t", @row ), "\n";
    Finally, print out a line for this row. So, in effect, only print out this exact list of data the first time it's encountered in the query results.

    We're building the house of the future together.
Re^3: Array of arrays
by fishbot_v2 (Chaplain) on Feb 25, 2006 at 16:34 UTC

    It looks complete to me. I would have joined on $; rather than "\0", but that's a difference of little consequence.

    How about you try it out, read up on the things that are immediately confusing to you, and try adding comments to the code as you go along? If you end up still stumped by something in these few lines of code, then as us.

    The entire "logic" of this code is in these two lines:

    my $krow = join "\0", @row; $seen{$krow}++ and next;

    Why don't you tell us what that does. If you re-write it like this:

    my $row_as_string = join "\0", @row; next if $seen{$row_as_string}++;

    Then read it aloud, it should be pretty clear.