Stripping duplicates from a list

Barry Walsh baz at draegtun.com
Thu Jun 12 16:57:06 BST 2008


Hows about......

    my @list = sort { uc($a) cmp uc($b) || $a cmp $b } <DATA>;
    my %seen;
    for ( @list ) {
        print $_ unless $seen{ uc $_ }++;
    }

    __DATA__  
    aubergine
    Aubergine
    Banana
    carrot
    Daikon
    DaiKon
    daikon
    elephant

Above gets rids of all duplicates (case insensitive) but gives 
precedence to capitalised ones because of the ASCIIbetical sort (so 
don't necessary trust it on non english data ;-)

--

/I3az/


Kake L Pugh wrote:
> Hello.  I have a list of strings of variable capitalisation, for example:
>
> aubergine
> Aubergine
> Banana
> carrot
> Daikon
> DaiKon
> daikon
> elephant
>
> What I want to do is strip out the duplicates - i.e. those strings
> which differ only by capitalisation - but keep by preference one of
> the versions that has a capital letter at the beginning.  So the above
> list would map to:
>
> Aubergine
> Banana
> carrot
> [either Daikon or DaiKon - doesn't matter which]
> elephant
>
> Ideas for the neatest way to do this?  Everything I've come up with so
> far is clumsy or buggy.  (This might be because I'm slightly hungover,
> so sorry if this is actually a really simple problem.)
>
> Kake
>
>
>   



More information about the london.pm mailing list