Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

counting inside the loop

by sarvan (Sexton)
on Jun 30, 2011 at 12:38 UTC ( #912175=perlquestion: print w/replies, xml ) Need Help??

sarvan has asked for the wisdom of the Perl Monks concerning the following question:


I have been suggested early on my script. Now the script work in the manner i wanted it. I want a slight modification to that.

se 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";
It is a is a guide is a guide a guide to guide to action ensures that the that the military No of matching is : 7

In addition to this i want to count the maximum no.of occurence for each matched combination(three elements).

i.e for eg. if "is a guide" appears two times in $str2. its maximum reference count is 2. likewise i want to compute for all the combinations.

my $count=($str2=~ tr/$t1//);

this counts the no. of times $t1 appeared. But i don't know where to put this inside the loop, so that it can be calculated for all the dynamic values of $t1.

Need help in this.. Thanks..

Replies are listed 'Best First'.
Re: counting inside the loop
by jethro (Monsignor) on Jun 30, 2011 at 13:03 UTC

    This will have the number of occurences of each combination stored in a hash. Think about how it works and you will know where to put it. If not, try out a few locations and observe what happens. Don't forget to print out the hash after all the loops are finished.

    my $count=($str2=~ tr/$t1//);

    This will count how often the characters in $t1 are in $str2, i.e. if $t1 were "ab" it would count all 'a's and 'b's in $str2. Is that really what you want?

      my $count=($str2=~ tr/$t1//);

      This will count how often the characters in $t1 are in $str2 ...

      Unfortunately, the  tr/// operator does not interpolate, but the  s/// or, better yet, the  m// operator can serve here:

      >perl -wMstrict -le "my $s = 'foo bar fooo bar foooo bar'; my $cc = 'foz'; ;; my $count = ($s =~ tr/$cc//); print $count; ;; $count = '$c$c$' =~ tr/$cc//; print $count; ;; $count = $s =~ tr/foz//; print $count; ;; $count = $s =~ s{ ([$cc]) }{$1}xmsg; print $count; ;; $count =()= $s =~ m{ [$cc] }xmsg; print $count; ;; $count = $s =~ s{ [$cc] }{}xmsg; print $count; print qq{'$s'}; " 0 5 12 12 12 12 ' bar bar bar'

      Note: There is no need for a capture group in the  s/// expression if characters in the string can be counted 'destructively':
          $count = $s =~ s{ [$cc] }{}xmsg;

      Update: Added  s/// and  m// examples, discussion.

      Hi, For this,
      $count=($str=~ tr/($t1)//);

      if $t1 has ab. i just want to count how many times ab is their in $str. together. not as seperately 'a' 'b'.

      What can be done for that..
Re: counting inside the loop
by rovf (Priest) on Jun 30, 2011 at 12:53 UTC
    The usual way of computing a maximum is to keep a current maximum (for instance initialized to zero), and, whenever you encounter a value which is larger than the current maximum, adjust the current maximum.

    Ronald Fischer <>

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://912175]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2023-09-25 09:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found