in reply to filter array based on 2 fields

@arr=("c1,s1,d1","c2,s2,d2","c1,s1,d2","c1,s2,d3"); print "\nWith duplicates: \n". join("\n",@arr); foreach $elem (@arr) { $elem=~m/(\w+,\w+)(,\w+)/; #Input assumed sane $seen{$1}=$2; } #Now you have a hash with unique (client,status) combos #Rebuild your array @arr=(); print "\n"; #Rebuild you array, without duplicate entries foreach $key (keys %seen) { push(@arr,$key . $seen{$key}); } #Note that order of elemens varies because of hash print "\nWith duplicates removed: \n". join("\n",@arr);

Replies are listed 'Best First'.
Re^2: filter array based on 2 fields
by hbm (Hermit) on Aug 20, 2012 at 14:30 UTC

    More simply - and keeping the order:

    @arr = ('c1,s1,d1','c2,s2,d2','c1,s1,d2','c1,s2,d3'); @arr = grep{ /(.+),/; !$seen{$1}++ } @arr; print join$/,@arr;

    Prints:

    c1,s1,d1 c2,s2,d2 c1,s2,d3
      Hi it's an array like this (client,Status,Description client,Status,Description and so on) it's lines for a csv file I need to remove duplicates based on client and status as description can vary because of a timestamp Thanks Thorbjorn

        Hi
        it's an array like this
        (client,Status,Description
        client,Status,Description
        and so on)
        it's lines for a csv file I need to remove duplicates based on client and status as description can vary because of a timestamp
        Thanks Thorbjorn