Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

(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 = = = = = = = = = = = = = = = = = = =
Life is short, but it's wide -- Chuck Pyle

In reply to Matching or masking specific characters in an array of strings by duggles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-26 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found