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:
(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.)#!/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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Comparing Lines within a Word List
by hippo (Bishop) on Apr 27, 2016 at 08:53 UTC | |
Re^2: Comparing Lines within a Word List
by dominick_t (Acolyte) on Apr 27, 2016 at 03:57 UTC | |
by graff (Chancellor) on Apr 27, 2016 at 10:07 UTC | |
by dominick_t (Acolyte) on Apr 27, 2016 at 14:58 UTC | |
Re^2: Comparing Lines within a Word List
by Anonymous Monk on Apr 27, 2016 at 15:22 UTC | |
Re^2: Comparing Lines within a Word List
by dominick_t (Acolyte) on Apr 27, 2016 at 15:33 UTC | |
Re^2: Comparing Lines within a Word List
by dominick_t (Acolyte) on Apr 29, 2016 at 23:04 UTC | |
by hippo (Bishop) on Apr 30, 2016 at 10:04 UTC | |
by dominick_t (Acolyte) on Apr 30, 2016 at 14:06 UTC | |
by graff (Chancellor) on Apr 30, 2016 at 15:27 UTC | |
by hippo (Bishop) on Apr 30, 2016 at 14:26 UTC | |
by dominick_t (Acolyte) on Apr 30, 2016 at 14:49 UTC | |
|
In Section
Seekers of Perl Wisdom