in reply to Re^3: Keeping Unique Elements in Array!
in thread Keeping Unique Elements in Array!

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?
  • Comment on Re^4: Keeping Unique Elements in Array!

Replies are listed 'Best First'.
Re^5: Keeping Unique Elements in Array!
by almut (Canon) on May 01, 2009 at 19:03 UTC

    There are several ways, for example Scalar::Util has looks_like_number(), or Regexp::Common's $RE{num}{real}.  Alternatively, in your specific case, you might just see if treating it as a number evaluates to zero, and then do a string comparison... (the latter wouldn't work if you also have negative numbers, though)

Re^5: Keeping Unique Elements in Array!
by Anonymous Monk on May 01, 2009 at 19:00 UTC
    Is this acceptable?
    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 +strict # Check if numeric if ($all_val->[$i] =~ /^-?\d/){ $all_val->[$i] = $row if $row > $all_val->[$i]; }else{ $all_val->[$i] = $row if $row gt $all_val->[$i]; } $i++; } } print Dumper $all_val;