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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:59:45+00:00 2026-05-30T22:59:45+00:00

I am quite new to Perl, and I am writing a Perl script. A

  • 0

I am quite new to Perl, and I am writing a Perl script. A part of my script counts the number of times each word is appearing in the text file. THIS COUNTING REPEATS AFTER SPECIFIC INTERVALS, SO I NEED AN ARRAY FOR EACH OF THAT REPEATING SEQUENCE. I have the code to count the number of words, BUT FOR JUST ONE SEQUENCE.

for (@array) {
  $counts{$_}++;
  print "\'$_\'\t";
}

My trouble is that I need to create an array for the hash “counts”.

EDIT: By ARRAY I mean that I should be able to store the repetition for each word for each particular section of the text file. I JUST NEED TO DETERMINE THE PARTIAL COUNT FOR EACH SECTION IN THE TEXT FILE. This is what my text file looks like:!

i HAVE uploaded an image to describe in details

  • 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-30T22:59:46+00:00Added an answer on May 30, 2026 at 10:59 pm

    The great thing about Perl is that there’s no need to initialize a hash or an array, you simply create one.

    You say you’re a new Perl user, but you seem to know about references. You can read an excellent tutorial right inside the Perl documentation. You can do this by using the perldoc command from your command line.

    That said, and looking at your application, I can see several different types of data structures:

    • Array of Hashes: In this structure, you’d have an array for each section you’re hitting, and you’d number the sections from 0 to the maximum you hit. You store the count of each entry in a hash for that entry.

    The code would look something like this:

    my $section_number = -1;   #We'll increment this to one on the first section number
    my @data;                  #This is an array where you'll store your sections
    
    while (my $line = <$my_file>) {
       chomp $line;
       if ($line =~ /^>this is my \w+ statement$/) {
           $section_number++;
           $data[$section_number] = {};  #A reference to a hash
       }
       else {
          $data[$section_number]->{$line}++;
       }
    }
    

    The first part of the if statement is merely incrementing the section count, so that each parameter is stored in a different section. This is nice if the question is In section #x, how many times did you see Parameter “y”?.

    • A Hash of Arrays: This time, you’re keeping track of the parameter, then the section that parameter appears in. This is the reverse of the above, but is good for answering the question How many times did parameter “y” appear in each section?

    The code would look something like this:

    my $section_number = -1;   #We'll increment this to one on the first section number
    my %data;                  #This is an array where you'll store your sections
    
    while (my $line = <$my_file>) {
       chomp $line;
       if ($line =~ /^>this is my \w+ statement$/) {
           $section_number++;
       }
       else {
           if (not exists $data{$line}) {
                $data{$line} = [];    #This hash will contain a ref to an array
           }          
           $data{$line}->[$section_number]++;
       }
    }
    

    Another possibilities is to use a Hash of Hashes that TLP showed.

    The point is that when you are talking about structure that contains more than mere scalar data, you need to use references.

    How you want to construct your data structure is really up to you and depends upon what you want to track and how you want to access that data. As shown in this one question, there are at least three different ways you could structure your data. And, building this complex data structure is fairly easy. And, there’s really nothing to initialize.

    Once you understand references, your data structure can be as complex as you dare (although I suggest to start looking into object oriented Perl coding techniques before you really go wild with them).

    By the way, none of the answers mentioned how you’d access your data besides using Data::Dumper, but a simple loop would be sufficient. This is for an array of hashes:

     my $section = 0;
     while ($section <= $#data) {
        my %param_hash = %{$data[$section]};
        foreach my $parameter (sort keys %param_hash) {
            print "In section $section: $parameter appears $param_hash{$parameter} times\n";
        }
        $section++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm dealing with a Perl script. Perl is quite new to me. Does anybody
I'm writing Perl for quite some time now and always discovering new things, and
Still quite new to Haskell.. I want to read the contents of a file,
I'm quite new to CSS / HTML, and can't find the cause of this
I am quite new in Perl and I woud like to know which of
I'm quite new to Perl development, and I'd like to perform a following task:
I'm quite new in perl, so I'm asking for tips for the problem below
Quite new to this mocking thing, I have a couple of questions. Correct me
I'm quite new to Perl and I'm trying to build a hash recursively and
I am quite new to Perl and have figured out how to symlink files.

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.