Stripping duplicates from a list

Nicholas Clark nick at ccl4.org
Thu Jun 12 16:41:09 BST 2008


On Thu, Jun 12, 2008 at 04:29:41PM +0100, Roger Burton West wrote:
> On Thu, Jun 12, 2008 at 04:16:17PM +0100, Kake L Pugh wrote:
> 
> >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.)
> 
> Assuming you're using a character set with a sane sorting order...

That's neater than mine, but mine quickly got modified to cope with insane
sorting orders:

$ cat ~/tmp/Kake.pl 
#!perl -w
use strict;

my (%ideal, %all);

while (<DATA>) {
  chomp;
  my $lc = lc $_;
  $all{$lc} = $_;
  $ideal{$lc} = $_ if /^\p{isUpper}/;
}

@all{keys %ideal} = values %ideal;

print "$all{$_}\n" foreach sort keys %all;

__END__
aubergine
Aubergine
Banana
carrot
Daikon
DaiKon
daikon
elephant


Seems to work:

$ perl ~/tmp/Kake.pl 
Aubergine
Banana
carrot
DaiKon
elephant

No idea what \p{isUpper} thinks of titlecase.


Nicholas Clark


More information about the london.pm mailing list