in reply to Keeping Unique Elements in Array!

...and for each not equal element keep the greatest one.

So if I'm understanding correctly, you want to apply some kind of max() function on the corresponding row values.

use strict; use warnings; use Data::Dumper; my $array_ref_x = [ [ 'ALICE WONDER', '9876543', '2009', '11.00', '711', '0', '0', '8' ], [ 'ALICE WONDER', '9876543', '2009', '11.00', '711', '3', '2', '0' ], ]; my $all_val = [ @{$array_ref_x->[0]} ]; my $i = 0; for my $row (@{$array_ref_x->[1]}) { $all_val->[$i] = $row if $row gt $all_val->[$i]; # assign greates +t $i++; } print Dumper $all_val; __END__ $VAR1 = [ 'ALICE WONDER', '9876543', '2009', '11.00', '711', '3', '2', '8' ];

Use > instead of gt of your understanding of "greatest" is in the numeric sense.  Also, you could of course handle more than two arrays similarly by putting another loop around it...

Replies are listed 'Best First'.
Re^2: Keeping Unique Elements in Array!
by Anonymous Monk on May 01, 2009 at 18:38 UTC
    You are mentioning that if this is added to the array:
    [ 'ALICE WONDER', '9876543', '2009', '15.00', '711', '4', '5', '9' ],

    another loop has to be added to the code? What if you dont know the number of elements in this array?

      What I meant is something like this, in which case you can have any number of arrays with any number of rows:

      my $all_val = []; for my $aref (@$array_ref_x) { my $i = 0; for my $row (@$aref) { $all_val->[$i] = '' unless defined $all_val->[$i]; # for 'use + warnings' $all_val->[$i] = $row if $row gt $all_val->[$i]; $i++; } }
        That's a great answer, one more question since you mentioned numeric values and of course use > for numeric ones, is there a way in Perl to detect and do the evaluation on both cases, if it is numeric or not on the same array like the sample code above?
        It does not scale like I thought, I was think that it could analize it by lets say the first and second elements and do the rest, here is a sample of what I am sying:
        my $array_ref_x = [ [ 'ALICE WONDER', '9876543', '2009', '11.00', '711', '20', '0', '8', ], [ 'ALICE WONDER', '9876543', '2009', '11.00', '711', '3', '2', '0', ], [ 'ALICE WONDER', '9876543', '2009', '15.00', '711', '4', '5', '11', ], [ 'ALICE WONDER', '9876543', '2008', '15.00', '717', '4', '5', '9', ], [ 'John Day', '456789', '2011', '50.00', '717', '4', '22', '9', ], [ 'John Day', '456789', '2011', '50.00', '817', '4', '5', '90', ], [ 'Mary Ann', '123988', '2002', '10.00', '119', '5', '3', '0', ], ]; #Results my $array_ref_x = [ [ 'ALICE WONDER', 9876543, 2009, '15.00', 717, 20, 5, 11 ], [ 'John Day', '456789', '2011', '50.00', '817', '4', '22', '90', ], [ 'Mary Ann', '123988', '2002', '10.00', '119', '5', '3', '0', ], ];