in reply to Find anagrams

Note: Updated with jcwren's suggestion.

Just felt like making it more compact:
use strict; my %words; chomp(my @words = map(lc, <DATA>)); @words{@words}=undef; my %anagrams; push @{$anagrams{join '', sort(split //, $_)}}, $_ for keys %words; @$_>1 && print join(",",@$_),"\n" for values %anagrams; __DATA__ and Stain satin Not Ton one

Replies are listed 'Best First'.
(jcwren) RE: (2) Find anagrams
by jcwren (Prior) on Sep 21, 2000 at 04:52 UTC
    Here's my stab at it. This fixes the CR/LF problem (that being no one should have to care what format a file was saved on what machine, be it Windows, *nix, or Mac), and handles multiple words per line. Oh yea, and no local variables declared!

    I have this gut feeling that it can be reduced further, but I can't find it.
    #!/usr/local/bin/perl -w use strict; push @{$ARGV[0] {join '', sort split //}}, $_ for map {lc} map {split} + <DATA>; @$_>1 && print join (',', @$_), $/ for values %{$ARGV[0]}; __DATA__ and dna Stain satin Not in this life Ton one file
    --Chris

    e-mail jcwren