in reply to Comparing Lines within a Word List

If you're unsure of the Perl syntax, try to work the steps out in English.

Does the text file contain one word per line? Are any words in the file repeated? Is the list of letter pairs (R and S in your example) fixed or something the user provides with each execution?

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re^2: Comparing Lines within a Word List
by dominick_t (Acolyte) on Apr 27, 2016 at 04:59 UTC
    Thank you for the reply! The text file contains one word per line, and no words in the file repeated. You could basically think of the text file as a dictionary of standard English words. Ideally the user would be able to easily supply any letter pair each execution. I figured it would be easier for me just to see some code that will do the trick for the R/S pairing, and then I assumed it would be easy enough matter to add some lines that would allow the user to input any pair whatsoever.

      The Tutorials here will tell you everything you need to know about opening a file and reading the words therein. You will probably store them in an array. Here's a trick to detect when two words differ by only single letter:

      if (($word1 ^ $word2) =~ tr[\1-\255][] == 1) { ... }

      That works because the ^ operator performs an XOR function. Same letters in $word1 and $word2 become null values, and the tr function returns the count of how many matches it found. I told it to look for non-null values. If the count is 1, that's a word we want to look at.

      Update: should have looked at the other replies before I composed this one. Better and more complete answers already provided!

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)