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

hello monks,

I have a problem in filtering the array. with max. date. for corresponding id, my array is like.

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

requried output:

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

Thanks.

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

    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

      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

Re: How to filter the array with id of max. date.
by johngg (Canon) on Apr 10, 2006 at 14:19 UTC
    An alternative method would be to sort the array by ascending date within group and map the result onto a hash keyed by group. This would result in the last (newest) data entry for each group overwriting any older entries in the hash. Then you can push values from the hash onto the @res list. Like this

    use strict; use warnings; use Data::Dumper; our @array = ( ['ashok',15,'2006-01-01'], ['ashokpj',15,'2006-02-01'], ['ravi',56,'2006-03-01'], ['raja',56,'2006-02-01']); our %newest = ( map {($_->[1], $_)} sort { $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } @array); our @res = (); push @res, $newest{$_} for sort {$a <=> $b} keys %newest; print Dumper(\@res);

    When run produces the following

    $VAR1 = [ [ 'ashokpj', 15, '2006-02-01' ], [ 'ravi', 56, '2006-03-01' ] ];

    Cheers,

    JohnGG

    Update: Changed %sorted to %newest as the name is more meaningful.