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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:56:54+00:00 2026-05-14T19:56:54+00:00

i have a problem where I need to have an array as a value

  • 0

i have a problem where I need to have an array as a value in an associative array.

Go through the code below. Here I am trying to loop the files in a directory and it is more likely that more than 1 file can have the same ctrno. So, I would like to see what are all the files having the same ctrno. The code below gives error at “$ctrno_hash[$ctrno] = @arr;” in the else condition. The same case would be for if condition as well.

Am I following the right approach or could it be done differently?

sub loop_through_files
{
    $file = "@_";
    open(INPFILE, "$file") or die $!;
    #print "$file:$ctrno\n";
    while (<INPFILE>)
    {
       $line .= $_;
    }
    if ($line =~ /$ctrno/ )
    {
       print "found\n";
       if ( exists $ctrno_hash[$ctrno])
       {
          local @arr = $ctrno_hash[$ctrno];
          push (@arr, $file);
          $ctrno_hash[$ctrno] =  @arr;
       }
       else
       {
          local @arr;
          push(@arr, $file);
          $ctrno_hash[$ctrno] =  @arr;
       }
    }
}
  • 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-14T19:56:55+00:00Added an answer on May 14, 2026 at 7:56 pm

    I believe you want something like

    $ctrno_hash[$ctrno] =  \@arr;
    

    This will turn the array @arr into a array reference.

    You then refer to the previously pushed array reference with

    @{$ctrno_hash[$ctrno]}
    

    That is, if $array_ref is an array reference, the construct @{ $array_ref } returns the array to which the array reference points.

    Now, the construct $ctrno_hash[$ctrno] is not really a hash, but an ordinary array. In order to truly make it a hash, you need the curly brackets instead of the square brackets:

    @{$ctrno_hash{$ctrno} } = \@arr;
    

    And similarly, you later refer to the array with

    @{$ctrno_hash{$ctrno} }
    

    Now, having said that, you can entirly forgo the if ... exists construct:

    if ($line =~ /$ctrno/ )
    {
       print "found\n";
       push @{$ctrno_hash{$ctrno}}, $file
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't. You're at the mercy of the SecurityManager, if… May 14, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer If that's an unmodified snippet from your HTML file, it's… May 14, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer There are no tools that do this for Blackberry development.… May 14, 2026 at 9:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.