Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A simple approach would be to build two hashes from the strings, and then compare the hashes.

So you might do something like:
my %foo; my $string = 'Poet Blake had a milky white cat. He used to call it Pus +sy.'; for my $word (split /\s+/, $string) { $foo{$word}++; }
You do the same for the second string, and then to compare you simply iterate through one of the hashes and increment a counter if each word is present in the other hash. Something like so:
my $cnt; for my $word (keys %foo) { $cnt++ if $bar{$word}; }

To find the total number of words in either string, you simply count the number of keys in the hash, e.g.

my $word_count = scalar keys %foo;

And then it's just a simple calculation.
Obvious question is how does your calculation look if the two strings contain a different number of words? But I'm sure you can decide that.

hope this helps,

In reply to Re: Is it possible to find the matching words and the percentage of matching words between two texts? by McDarren
in thread Is it possible to find the matching words and the percentage of matching words between two texts? by supriyoch_2008

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2017-09-21 05:31 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (242 votes). Check out past polls.