Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It takes a little longer, but the result is a file that shows all words in your worlist which differ in one place only and they are sorted according to which characters are different.

For instance for "rs" differences, the result (based upon my wordlist) is as follows:

***** rs ***** abaser - abases abater - abates abider - abides abjurer - abjures ... year - yeas yodler - yodles yummier - yummies zanier - zanies zoner - zones
And here is the program:
use Modern::Perl qw/2015/; use List::MoreUtils qw/zip natatime/; use autodie; open( my $DICT, '<', './wordsEn.txt' ) or die "Could not open dictiona +ry - $!"; my @dict; print scalar <$DICT>; # show copyright message of word list # step 1: sort the dictionary into bins per length of the word while ( my $word = <$DICT> ) { chomp $word; next unless $word; push @{ $dict[ length $word ] }, $word; } close $DICT; # step 2: for each bin look for words that differ in one place only my %results; for my $length ( 2 .. @dict - 1 ) { next unless $dict[$length]; # skip if no words of this length my @bin = @{ $dict[$length] }; next if @bin < 2; # skip if only one word of this length say "*********** Testing words of length $length ***********"; while ( my ( $index, $test ) = each @bin ) { for my $check ( @bin[ $index + 1 .. @bin - 1 ] ) { my $diff = $test ^ $check; if ( 1 == $diff =~ tr/\x00//c ) { # only one character dif +ferent # find which characters are different my @first = split '', $test; my @second = split '', $check; my @test = zip @first, @second; my $it = natatime 2, @test; while ( my @vals = $it->() ) { next if $vals[0] eq $vals[1]; my $key = $vals[0] lt $vals[1] ? "$vals[0]$vals[1]" : "$vals[1]$vals[0]"; push @{ $results{$key} }, "$test - $check"; # save + in hash last; } } } } } say "Now writing results"; open( my $RESULTS, '>', './results.txt' ) or die "Cannot open output f +ile $!"; for my $key ( sort keys %results ) { say $RESULTS "***** $key *****"; for my $words ( sort @{ $results{$key} } ) { say $RESULTS "\t$words"; } } close $RESULTS;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

In reply to Re: Comparing Lines within a Word List by CountZero
in thread Comparing Lines within a Word List by dominick_t

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 having a coffee break in the Monastery: (3)
As of 2024-04-19 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found