in reply to Re: How to filter the array with id of max. date.
in thread How to filter the array with id of max. date.

Yes ur right it an Array of Aarray

my @array = ( ['ashok',15,'2006-01-01'], ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'], ['raja',56,'2006-02-01'], );

requried o/p

@res = ( ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'] );

because i want to get last update recode same time it must be group by ID (name,id,date(yyyy-mm-dd));

Replies are listed 'Best First'.
Re^3: How to filter the array with id of max. date.
by Zaxo (Archbishop) on Apr 10, 2006 at 06:43 UTC

    Ah, ok, that's clear now.

    You want the entries for latest date in each group id. Your date format is fortunate because it can be ordered alphabetically.

    use Data::Dumper; my @array = ( ['ashok',15,'2006-01-01'], ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'], ['raja',56,'2006-02-01'], ); my @res = do { my %group; for (@array) { # $group{$_->[1]} = $_ if $_->[2] gt $group{$_->[1]}; bad $group{$_->[1]} = $_ if $_->[2] gt $group{$_->[1]}[2]; } sort {$a->[1] <=> $b->[1]} values %group; }; print Dumper \@res; __END__ $VAR1 = [ [ 'ashokpj', 15, '2006-02-01' ], [ 'ravi', 56, '2006-03-01' ] ];
    That gives the @res you wanted. The sort call only puts the elements in group order. If you don't care about that you can replace the line with just values %group;

    Edit: Oops! I left an index off my comparison - corrected. Thanks, anon. Added dump.

    After Compline,
    Zaxo

      Dear Zaxo

      Your output doesn't match to ashokpj.

      ashokpj needs output

      @res = ( ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'] );
      but your out
      @res =([ 'ashok', 15, '2006-01-01' ], [ 'ravi', 56, '2006-03-01' ] );