It looks like you want to establish word similarity without regard to order or case. Try a hash (it will give you the flexibility to modify the rules of the game later).
my $word = 'bantha fodder'; my @letters = split//, lc($word); my %counts; for(@letters){ $counts{$_}++; } for(sort {$counts{$b} <=> $counts{$a}} keys %counts){ my $t = $_ =~ /[a-z]/ ? 'letter' : $_ =~ /[0-9]/ ? 'number' : 'character'; my $s = $counts{$_} > 1 ? 's' : ''; printf " the %-10s: %-3s ocurred %-3s time%s\n", $t,$_,$counts{$_},$s }
which prints
the letter : 'a' ocurred 2 times the letter : 'd' ocurred 2 times the letter : 'e' ocurred 1 time the letter : 'n' ocurred 1 time the letter : 'r' ocurred 1 time the character : ' ' ocurred 1 time the letter : 'h' ocurred 1 time the letter : 'b' ocurred 1 time the letter : 'f' ocurred 1 time the letter : 't' ocurred 1 time the letter : 'o' ocurred 1 time
Now wrap that in a subroutine and use some loop control:
sub letter_counts { my @letters = split//, lc(shift); my %counts; for(@letters){ $counts{$_}++; } return %counts } my $word1 = 'bantha fodder'; my $word2 = 'banana slug'; my %counts1 = letter_counts($word1); my %counts2 = letter_counts($word2); my $result = 'are equivalent'; test_loop: for(keys %counts1, keys %counts2){ if(!defined $counts2{$_} || !defined $counts1{$_}){ $result = 'are not equivalent'; last test_loop } } print "'$word1' and '$word2' $result\n";
Now you can modify the loop / conditional as you see fit.

For order dependent similarity, try Algorithm::Diff

In reply to Re: Word Comparison by mobiusinversion
in thread Word Comparison by rooneyl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.