http://qs321.pair.com?node_id=1161597


in reply to Comparing Lines within a Word List

If you're looking for a way to catch any two words that differ by any single letter (e.g. report if input contains "fool/foot" and/or "foot/loot" and/or "loot/lout"), then it's a matter of building on Eily's idea using bitwise xor on pairs of strings that are the same length. Look at the elements of the array returned by the "split" statement, and if only one element of the array is non-zero, then the given pair of strings differ by only one character. (You'll want to organize your input into groups based on word length, and only do comparisons within each group.)

OTOH, if you're looking for words that contain a particular pair of characters, and differ only in terms of using one vs. the other of those two (e.g. you really just want "bare/base", etc., but not "foot/fool"), you would probably want to use a regex like this:

#!/usr/bin/perl use strict; use warnings; my @words = <DATA>; chomp @words; while ( @words >= 2 ) { my $model = my $regex = shift @words; if ( $regex =~ s/(.*?)[rs](.*?)/$1\[rs\]$2/ ) { my @hits = grep /^$regex$/, @words; if ( @hits ) { print join( " ", $model, "matches", @hits, "using", $regex +, "\n" ); } } } __DATA__ bare mare base case bust burt bent sat rat bat matter mattes pat
(update: fixed the "while" condition as per hippo's remark below -- also added anchors around $regex in the grep call, so that "bare" doesn't match "debased", etc.)