Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: count the maximum no.of occurence

by sarvan (Sexton)
on Jun 30, 2011 at 07:03 UTC ( [id://912110]=note: print w/replies, xml ) Need Help??


in reply to Re: count the maximum no.of occurence
in thread count the maximum no.of occurence

use warnings; use strict; my $str1 = q/It is a guide to action which ensures that the military a +lways obey the commands of the party./; my $str2 = q/It is a guide to action that ensures that the military wi +ll forever heed Party commands is a guide./; my $n = 0; while ( $str1 =~ /(?=(\S+\s+\S+\s+\S+))/g ) { my $t1 = $1; while ( $str2 =~ /($t1)/g ) { print $1; $n++; } } print "No of matching is : $n";
but output of the above code comes like this:

It is at is ais a guideis a guides a guides a guidea guide toguide to actionuide to actionide to actionde to actione to actionensures that thensures that thesures that theures that theres that thees that thes that thethat the militaryhat the militaryat the militaryt the military

Not the expected output.. What i expected is:
It is a is a guide is a guide a guide to guide to action ensures that the that the military
And the maximum reference count of each word i want.. i.e in this case. "is a guide" is matched two times so its count is 2. Any suggestions.

Replies are listed 'Best First'.
Re^3: count the maximum no.of occurence
by jwkrahn (Abbot) on Jun 30, 2011 at 07:08 UTC

    Sorry, you need to add newlines, like so:

    use warnings; use strict; my $str1 = q/It is a guide to action which ensures that the military a +lways obey the commands of the party./; my $str2 = q/It is a guide to action that ensures that the military wi +ll forever heed Party commands is a guide./; my $n = 0; while ( $str1 =~ /(?=(\S+\s+\S+\s+\S+))/g ) { my $t1 = $1; while ( $str2 =~ /($t1)/g ) { print "$1\n"; $n++; } } print "No of matching is : $n\n";
      Hi jwkrahn, Again the output is something like this..
      It is a t is a is a guide is a guide s a guide s a guide a guide to guide to action uide to action ide to action de to action e to action ensures that the nsures that the sures that the ures that the res that the es that the s that the that the military hat the military at the military t the military

        Hi jwkrahn, Again the output is something like this..

        No it isn't. The code from jwkrahn produces the output you wanted. If it does not do that for you, then you are not executing that code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://912110]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found