Anagram.pm (739B) - raw
1 package Anagram; 2 use strict; 3 use warnings; 4 use Exporter qw<import>; 5 our @EXPORT_OK = qw<match_anagrams>; 6 7 sub is_anagram { 8 my ( $subject, $candidate ) = @_; 9 10 return 0 if ( $subject eq $candidate ); 11 return 0 if ( length( $subject ) != length( $candidate ) ); 12 13 my %subject_hash; 14 my %candidate_hash; 15 16 $subject_hash{ $_ } += 1 foreach ( split //, $subject ); 17 $candidate_hash{ $_ } += 1 foreach ( split //, $candidate ); 18 19 return 1 unless grep( $subject_hash{ $_ } != $candidate_hash{ $_ }, 20 keys %subject_hash ); 21 22 return 0; 23 } 24 25 sub match_anagrams { 26 my ($input) = @_; 27 my @output; 28 29 foreach ( @{ $input->{candidates} } ) { 30 push @output => $_ if is_anagram( lc $input->{subject}, lc $_ ); 31 } 32 33 return \@output; 34 35 } 36 37 1;