Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:34:50+00:00 2026-05-13T13:34:50+00:00

i need to implement a program to count the occurrence of a substring in

  • 0

i need to implement a program to count the occurrence of a substring in a string in perl. i have implemented it as follows

sub countnmstr
{
  $count =0;
  $count++ while $_[0] =~ /$_[1]/g;
  return $count;
}

$count = countnmstr("aaa","aa");

print "$count\n";

now this is what i would normally do. however, in the implementation above i want to count occurrence of ‘aa’ in ‘aaa’. here i get answer as 1 which seems reasonable but i need to consider the overlapping cases as well. hence the above case should give an answer as 2 since there are two ‘aa’s if we consider overlap.

can anyone suggest how to implement such a function??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:34:51+00:00Added an answer on May 13, 2026 at 1:34 pm

    See ysth’s answer … I failed to realize that the pattern could consist solely of a zero width assertion and still work for this purpose.

    You can use positive lookahead as suggested by others, and write the function as:

    sub countnmstr {
        my ($haystack, $needle) = @_;
        my ($first, $rest) = $needle =~ /^(.)(.*)$/;
        return scalar (() = $haystack =~ /(\Q$first\E(?=\Q$rest\E))/g);
    }
    

    You can also use pos to adjust where the next search picks up from:

    #!/usr/bin/perl
    
    use strict; use warnings;
    
    sub countnmstr {
        my ($haystack, $needle) = @_;
        my $adj = length($needle) - 1;
        die "Search string cannot be empty!" if $adj < 0;
    
        my $count = 0;
        while ( $haystack =~ /\Q$needle/g ) {
            pos $haystack -= $adj;
            $count += 1;
        }
        return $count;
    }
    
    print countnmstr("aaa","aa"), "\n";
    

    Output:

    C:\Temp> t
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement program by c# application. first of all I need web
I'm about to implement a small program where i need the elapsed seconds since
I need to implement in C the program ,which reads an unknown number of
I need to implement a handshake type protocol in to a small Linux program
i need to implement a Remember password option in my program, it works with
All, I have Core Data implemented in my program to store customer information. Let's
is it possible to convert a Python program to C/C++? I need to implement
I need to implement a program to handle a word correction / suggestion system.
I need to implement a custom scoring system in my database design. My program
I have no experience with sockets and I need to implement a process that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.