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

Why is "ashokpj" in your output, but not "raja"? Both have the same date.

I think we need a tighter spec to know what to produce.

In any case, your @array looks like you wanted an AoA instead:

my @array = ( ['ashok',15,'2006-01-01'], ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'], ['raja',56,'2006-02-01'], );
As you wrote it, your @array was a flat set of values. Fixing that will probably help.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How to filter the array with id of max. date.
by ashokpj (Hermit) on Apr 10, 2006 at 06:25 UTC

    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));

      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' ] );