duggles has asked for the wisdom of the Perl Monks concerning the following question:
(apologies in advance for this title - I couldn't think of a more explicit way to summarize the question.... ;-)
This code is used in a cryptogram solving script. After a pattern match for a word is pulled from a dictionary, I can usually find an exclusive single match, which happens often enough to make me happy, but many times there is a group of matches found that I've just ignored. But then it dawned on me that in some cases, especially those with just a few results, some of the letters DID match and I could use just those letters in my translation. I started working on a way of building a mask of just the matching letters.
The code below works, giving me a string mask (e.g. '-ou--e--' for 'yourself' and 'southern') and a hash containing the letters and positions that match, but I'm certain there must be a more efficient, faster, more elegant way to do this. I thought about a regex but that's a very weak area for me so I thought I'd throw this out there and see if I could learn something.
Any help towards my education is sincerely appreciated!
FYI and if it matters - I'm running on Windows XP with perl-5.10
#!/usr/bin/perl -w use strict; my %groups = ( group1 => ['cooling', 'rooting', 'hooting', 'looking', 'doormat', +'cooking', 'cookies', 'noodles'], # desired result "-oo----" group2 => ['yourself', 'southern'], # desired result "-ou--e--" group3 => ['arden', 'arlen', 'asked'] # desired result "a--e-" ); print " = = = = = = = = = = = = = = = = = = = \n\n"; foreach my $group (keys %groups) { my $mask = ''; my %matches = (); my @group = @{$groups{$group}}; foreach (@{$groups{$group}}) { print "\t$_\n"; } my $group_count = $#group; my $group_length = length $group[0]; print "group word count:$group_count group string length:$group_le +ngth\n"; for (my $i = 0; $i < $group_count +1; $i++) { for (my $j = 0; $j < $group_length; $j++) { if ($i) { if (substr ($group[$i],$j,1) eq substr ($group[$i-1],$ +j,1)) { print substr ($group[$i],$j,1); if (substr ($mask,$j,1) ne "-") { substr ($mask +,$j,1) = substr ($group[$i],$j,1); } $matches{$j}++ } else { print "-"; substr ($mask,$j,1) = "-"; } } } if ($i) { print "($group[$i-1])"; print " ($group[$i]) "; } print " $mask\n"; } print "\n"; print "letter\t\tposition\n"; foreach my $key (sort (keys(%matches))) { if ($matches{$key} == $group_count) { print substr($group[0],$key,1), " \t\t$key \n"; } } print "\n"; print " = = = = = = = = = = = = = = = = = = = \n\n"; }
OUTPUT: c:\mycode>perlmonk-code-example.pl = = = = = = = = = = = = = = = = = = = yourself southern group word count:1 group string length:8 -ou--e--(yourself) (southern) -ou--e-- letter position o 1 u 2 e 5 = = = = = = = = = = = = = = = = = = = cooling rooting hooting looking doormat cooking cookies noodles group word count:7 group string length:7 -oo-ing(cooling) (rooting) -oo-ing -ooting(rooting) (hooting) -oo-ing -oo-ing(hooting) (looking) -oo-ing -oo----(looking) (doormat) -oo---- -oo----(doormat) (cooking) -oo---- cooki--(cooking) (cookies) -oo---- -oo--es(cookies) (noodles) -oo---- letter position o 1 o 2 = = = = = = = = = = = = = = = = = = = arden arlen asked group word count:2 group string length:5 ar-en(arden) (arlen) ar-en a--e-(arlen) (asked) a--e- letter position a 0 e 3 = = = = = = = = = = = = = = = = = = =
|
---|