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

  • SEARCH
  • Home
  • 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 4266040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:42:22+00:00 2026-05-21T06:42:22+00:00

I am new to Perl, by the way. I have a Perl script that

  • 0

I am new to Perl, by the way. I have a Perl script that needs to count the number of times a string appears in the file. The script gets the word from the file itself.

I need it to grab the first word in the file and then search the rest of the file to see if it is repeated anywhere else. If it is repeated I need it to return the amount of times it was repeated. If it was not repeated, it can return 0. I need it to then get the next word in the file and check this again.

I will grab the first word from the file, search the file for repeats of that word, grab the second word from
the file, search the file for repeats of that word, grab the third word from the file, search the file for repeats of that word.

So far I have a while loop that is grabbing each word I need, but I do not know how to get it to search for repeats without resetting the position of my current line. So how do I do this? Any ideas or suggestions are greatly appreciated! Thanks in advance!

while (<theFile>) {
    my $line1 = $_;
    my $startHere = rindex($line1, ",");
    my $theName = substr($line1, $startHere + 1, length($line1) - $startHere);
    #print "the name: ".$theName."\n";
}
  • 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-21T06:42:23+00:00Added an answer on May 21, 2026 at 6:42 am

    Use a hashtable;

    my %wordcount = ();
    
    while(my $line = <theFile>)
    {
        chomp($line);
        my @words = split(' ', $line);
        foreach my $word(@words)
        {
            $wordCount{$word} += 1;
        }
    }
    
    # output
    foreach my $key(keys %wordCount)
    {
        print "Word: $key Repeat_Count: " . ($wordCount{$key} - 1) . "\n";
    }
    

    The $wordCount{$key} - 1 in the output accounts for the first time a word was seen; Words that only apprear once in the file will have a count of 0

    Unless this is actually homework and/or you have to achieve the results in the specific manor you describe, this is going to be FAR more efficient.

    Edit: From your comment below:

    Each word i am searching for is not “the first word” it is a certain word on the line. Basically i have a csv file and i am skipping to the third value and searching for repeats of it.

    I would still use this approach. What you would want to do is:

    • split on , since this is a CSV file
    • Pull out the 3rd word in the array on each line and store the words you are interested in in their own hash table
    • At the end, iterate through the “search word” hash table, and pull out the counts from the wordcount table

    So:

    my @words = split(',', $line);
    $searchTable{@words[2]} = 1;
    
    ...
    
    foreach my $key(keys %searchTable)
    {
        print "Word: $key Repeat_Count: " . ($wordCount{$key} - 1) . "\n";
    }
    

    you’ll have to adjust according to what rules you have around counting words that repeat in the third column. You could just remove them from @words before the loop that inserts into your wordCount hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that makes barplots, and opens a new window when 6
I have a Perl script which forks a number of sub-processes. I'd like to
I have a perl script that uses the CGI::Session::Drive::memcached, but I want to be
I have a Perl script that's trying to set some configured DateTime and DateTime::Duration
I have a Perl script that uses Selenium to fetch a HTML document called
I am new to C++ coding. I wanted to execute a Perl script (that
I'm still learning Perl and CLASS::DBI. I have a script that does a bunch
I have a perl script that is used to monitor databases and I'm trying
Say I have a script written in perl or python. What's the easiest way
I have a Perl module (Module.pm) that initializes a number of variables, some of

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.