From 8b2112768ba5be6dd7e33b56a4052514244fa850 Mon Sep 17 00:00:00 2001 From: gdrooid Date: Thu, 3 Jul 2014 15:50:36 +0200 Subject: [PATCH] Anagram: read words only once. --- lib/DDG/Goodie/Anagram.pm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/DDG/Goodie/Anagram.pm b/lib/DDG/Goodie/Anagram.pm index a92dec8ea..c05dd4999 100644 --- a/lib/DDG/Goodie/Anagram.pm +++ b/lib/DDG/Goodie/Anagram.pm @@ -20,6 +20,8 @@ topics "words_and_games"; attribution github => ["https://github.com/loganom", 'loganom'], github => ["https://github.com/beardlybread", "beardlybread"]; +my @words = share('words')->slurp; + handle remainder => sub { s/^of\s(.*)/$1/i; @@ -49,19 +51,17 @@ handle remainder => sub { } } - my $fileobj = share("words"); # list of words - open my $INF, "<", $fileobj->stringify or return; #read in the words file - while (<$INF>) { # while we have more input + foreach (@words) { # while we have more input # if $word has a value and the text input contains characters from our word and is the correct length if ($word and /^[$word]{$n}$/i) { chomp; # skip if the word we see is the original word - next if lc($_) eq lc($word); + next if lc eq lc($word); # split the word by character, counting frequency of each character # the words here come from the list of words file my %f; - for (split //, lc($_)) { + for (split //, lc) { if ($f{$_}) { $f{$_} += 1; } else {