Stripping duplicates from a list

Adam Bernard ab11+londonpm at sanger.ac.uk
Thu Jun 12 16:45:46 BST 2008


2008/6/12 Kake L Pugh <kake at earth.li>:
> 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

I think
perl -lne'$h{lc($_)}=$_ unless $h{lc($_)} =~ /^[A-Z]/;END{print for
sort values %h}'
does it, but you will want to test it better than I have.

Adam


More information about the london.pm mailing list