Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Array of arrays

by Win (Novice)
on Feb 21, 2006 at 17:39 UTC ( #531743=perlquestion: print w/replies, xml ) Need Help??

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

Good Monks. I have the following bit of code:
while (my @row = $sth_C->fetchrow_array) { print OUTPUT_FILE join("\t", @row); print OUTPUT_FILE "\n"; undef @row; }
Which I would like modified to check to see whether the array @row is within an array of arrays called @collection_of_rows. If it is I want it to exit the loop and if it isn't I want it to continue as before and add @row to the array of arrays.

Replies are listed 'Best First'.
Re: Array of arrays
by Roy Johnson (Monsignor) on Feb 21, 2006 at 17:50 UTC
    So you just need a function to tell you whether two arrays are equal. Here ya go:
    sub arrays_eq (\@\@) { my ($a1, $a2) = @_; # Same length? return 0 unless @$a1 == @$a2; # Same contents? for (0..$#$a1) { return 0 if $a1->[$_] ne $a2->[$_]; } return 1; }

    Caution: Contents may have been coded under pressure.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Array of arrays
by GrandFather (Saint) on Feb 21, 2006 at 17:47 UTC

    An AoA is an array of scalars holding references to arrays. So assuming that you either have an AoA or a simple array, you can:

    last if ref $array[0];

    DWIM is Perl's answer to Gödel
Re: Array of arrays
by jdporter (Chancellor) on Feb 21, 2006 at 18:41 UTC

    Well that could be an expensive proposition. If you can live with the memory cost, something like the following will work. In this version, I'm presuming that the "collection of rows" starts out empty, and that you don't need it for anything other than to determine whether a row has been seen before or not. If that presumption is false, let me know and I can enhance it accordingly.

    my %seen; my @row; while ( @row = $sth_C->fetchrow_array ) { my $krow = join "\0", @row; $seen{$krow}++ and next; # or did you mean last? print OUTPUT_FILE join( "\t", @row ), "\n"; undef @row; }
    We're building the house of the future together.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://531743]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-01-23 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?