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 8698825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:48:26+00:00 2026-06-13T01:48:26+00:00

The question is: Perl script that counts the number of times each digit appears

  • 0

The question is:
Perl script that counts the number of times each digit appears in the given input. Print the total for each digit and the sum of all the totals.

The script is:

#!/usr/bin/perl

my $str = '654768687698579870333';

if ($str =~ /(.*)[^a]+/) {

    my $substr = $1;
    my %counts;

    $counts{$_}++ for $substr =~ /./g;

    print "The count each digit appears is: \n";
    print "'$_' - $counts{$_}\n" foreach sort keys %counts;
    my $sum = 0;
    $sum += $counts{$_} foreach keys %counts;
    print "The sum of all the totals is $sum\n";    
}

The Output I am getting is:

The count each digit appears is:
'0' - 1
'3' - 2
'4' - 1
'5' - 2
'6' - 4
'7' - 4
'8' - 4
'9' - 2
The sum of all the totals is 20

But the output I am supposed to get is:

The count each digit appears is:
'0' - 1
'3' - 3
'4' - 1
'5' - 2
'6' - 4
'7' - 4
'8' - 4
'9' - 2
The sum of all the totals is 21

Where am I going wrong? Please help. Thanks in advance

  • 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-06-13T01:48:27+00:00Added an answer on June 13, 2026 at 1:48 am

    Instead of the inspecting the entire string ($str), you inspect all but the last character of it ($substr).

    if ($str =~ /(.*)[^a]+/) {
        my $substr = $1;
    
        my %counts;
        $counts{$_}++ for $substr =~ /./g;
    

    should be

    my %counts;
    ++$counts{$_} for $str =~ /[0-9]/g;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Perl script that runs 4 simultaneous, identical processes with different input
I have a question about Perl from the script below. If the input for
First question from a long time user. I'm writing a Perl script that will
My question in Perl: Write a Perl Script with a subroutine that accepts a
I have written a Perl script for the following bioinformatics question, but unfortunately there
I am writing a script in Perl and have a question about Perl's foreach
the following perl script (Showing down) print the following XML file but the XML
I have a Perl script that does a fork/exec to start another tool in
I have seen a usage perl script used to pre-split data Link Question> How
I have a simple Perl script that prints out @INC as the following: #!/usr/bin/perl

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.