Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the word with most letters in common with other words
    primarykey
    data
    text
    <p>I want Perl (5.8.8) to find out what word has the most letters in common with the other words in an array - but only letters that are in the same place. (And preferably without using libs.)</p> <p>Take this list of words as an example:</p> <ul> <li>BAKER</li> <li>SALER</li> <li>BALER</li> <li>CARER</li> <li>RUFFR</li> </ul> <p>Her BALER is the word that has the most letters in common with the others. It matches BAxER in BAKER, xALER in SALER, xAxER in CARER, and xxxxR in RUFFR. </p> <p>I want Perl to find this word for me in an arbitrary list of words with the same length and case. Seems I've hit the wall here, so help is much appreciated!</p> <h2>What I've tried until now</h2> <p>Don't really have much of a script at the moment:</p> <pre><code>use strict; use warnings; my @wordlist = qw(BAKER SALER MALER BARER RUFFR); foreach my $word (@wordlist) { my @letters = split(//, $word); # now trip trough each iteration and work magic... } </code></pre> <p>Where the comment is, I've tried several kinds of code, heavy with for-loops and ++ varables. Thus far, none of my attempts have done what I need it to do.</p> <p><strong>So, to better explain: What I need is to test word for word against the list, for each letterposition, to find the word that has the most letters in common with the others in the list, at that letter's position.</strong></p> <p>One possible way could be to first check which word(s) has the most in common at letter-position 0, then test letter-position 1, and so on, until you find the word that in sum has the most letters in common with the other words in the list. Then I'd like to print the list like a matrix with scores for each letterposition plus a total score for each word, not unlike what DavidO suggest. </p> <p>What you'd in effect end up with is a matrix for each words, with the score for each letter position, and the sum total score fore each word in the matrix.</p> <h2>Purpose of the Program</h2> <p>Hehe, I might as well say it: The program is for hacking terminals in the game Fallout 3. :D My thinking is that it's a great way to learn Perl while also having fun gaming.</p> <p>Here's one of the Fallout 3 terminal hacking tutorials I've used for research: <a href="http://www.gamefaqs.com/pc/918428-fallout-3/faqs/54644/" rel="nofollow">FALLOUT 3: Hacking FAQ v1.2</a>, and I've already made a program to shorten the list of words, like this: </p> <pre><code>#!/usr/bin/perl # See if one word has equal letters as the other, and how many of them are equal use strict; use warnings; my $checkword = "APPRECIATION"; # the word to be checked my $match = 4; # equal to the match you got from testing your checkword my @checkletters = split(//, $checkword); #/ my @wordlist = qw( PARTNERSHIPS REPRIMANDING CIVILIZATION APPRECIATION CONVERSATION CIRCUMSTANCE PURIFICATION SECLUSIONIST CONSTRUCTION DISAPPEARING TRANSMISSION APPREHENSIVE ENCOUNTERING ); print "$checkword has $match letters in common with:\n"; foreach my $word (@wordlist) { next if $word eq $checkword; my @letters = split(//, $word); my $length = @letters; # determine length of array (how many letters to check) my $eq_letters = 0; # reset to 0 for every new word to be tested for (my $i = 0; $i &lt; $length; $i++) { if ($letters[$i] eq $checkletters[$i]) { $eq_letters++; } } if ($eq_letters == $match) { print "$word\n"; } } # Now to make a script on to find the best word to check in the first place... </code></pre> <p>This script will yield <code>CONSTRUCTION</code> and <code>TRANSMISSION</code> as its result, just as in the game FAQ. The trick to the original question, though (and the thing I didn't manage to find out on my own), is how to find the best word to try in the first place, i.e. <code>APPRECIATION</code>.</p> <p><em>OK, I've now supplied my own solution based on your help, and consider this thread closed. Many, many thanks to all the contributers. You've helped tremendously, and on the way I've also learned a lot.</em> :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload