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

Hi Monks,

I am storing the result of a query in an array. my array looks like

(["a","b","c"] ["a","b","d"])

I want to compare the elements of the first row with the corresponding elements of the second row, and if the first 2 sets matches i want to display

a b c,d

with my current code my result look like

a b b a b d

following is my code

my arr_ref= $sth->fetchall_arrayref(); foreach my $row(@$arr_ref) { #missing something here my ($ele1,$ele2,$ele3)= @$row; print "$ele1\t", print "$ele2\t", print "$ele3\n", }

Replies are listed 'Best First'.
Re: how to compare the rows in a 2d array
by Corion (Patriarch) on Mar 04, 2010 at 15:40 UTC

    What is the rule for printing? Maybe you want the following:

    my @last_key; for my $row (@$arr_ref) { local $" = "\0"; my @curr_key = @{$row}[0,1]; if ("@curr_key" ne "@last_key") { { @last_key = @curr_key; print join "\n", @curr_key; print "$row->[2]\n"; } else { print ",", $row->[2]; }; };

    There is one problem with my solution. I was too lazy to shield the code against \0 occurring in your data. If that is a possibility, you will have to modify my code to do a comparison by-element instead of relying on Perls stringification of arrays to do the magic.

Re: how to compare the rows in a 2d array
by almut (Canon) on Mar 04, 2010 at 15:55 UTC

    (If I'm understanding you correctly — not exactly sure what you mean by "sets"...)

    #!/usr/bin/perl my @arr = (["a","b","c"], ["a","b","d"]); my ($r1, $r2) = @arr; my ($m1, $m2); for my $i (0..$#$r1) { if ($m1 and $m2) { print "$m2\n$m1\n$r1->[$i],$r2->[$i]\n"; } $m2 = $m1; $m1 = $r1->[$i] eq $r2->[$i] ? $r1->[$i] : undef; } __END__ a b c,d

    This keeps track of the previous two comparisons, and prints them plus the current/next one if both previous ones match.

    I chose this implementation because I wasn't quite sure if your rows might potentially hold more than 3 elements, and the two consecutive equal ones could occur at any position. IOW, the approach would work, too, for input such as

    (["a","c","a","b","c",...], ["a","d","a","b","d",...])
Re: how to compare the rows in a 2d array
by youlose (Scribe) on Mar 04, 2010 at 19:03 UTC
    use Modern::Perl; use Array::Utils qw(array_diff intersect); my @res_of_q = ( [qw(a b c)], [qw(a b d)] ); say join "\n",intersect( @{$res_of_q[0]}, @{$res_of_q[1]} ); say join ',',array_diff( @{$res_of_q[0]}, @{$res_of_q[1]} );
    P.S. shitty parser... =(
Re: how to compare the rows in a 2d array
by ack (Deacon) on Mar 04, 2010 at 20:34 UTC

    I like the solutions others ofered. I was taking your post literally and solved your specific problem, but it doesnt scale or generalize like the other responders resposes do. So I'm not going to obuscate their good responses with my code.

    A couple of minor observations. Your code as posted does not work nor does it produce the output you say it does. First, in the first line you are missing the sigil '$' before arr_ref. Second, all three print lines end with a comma ',' rather than a semi-colon ';'...thus causing the prints to get all fowled up. As I said, those are just four your information; I and the rest of the responders pretty much knew what you probably meant.

    ack Albuquerque, NM