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

The Archive Base Latest Questions

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

This may be a simple oversight on my part (or something much more advanced

  • 0

This may be a simple oversight on my part (or something much more advanced than my skill set). I am trying to dynamically fill a 2d associative array by reading input from a file.

my @data;
while (<FILE>) {
    chomp;

    my $ID,$COUNT;
    print "READ: " . $_ . "\n"; #Debug 1

    ($ID,$COUNT,undef,undef,undef) = split /\,/;
    print "DATA: " . $ID . "," . $COUNT . "\n"; # Debug 2

    $data{$ID}{"count"} = $COUNT;
    #push @{$data{$ID}{"count"}}, $COUNT; 

              print $data{$ID}{"count"} . "\n"; # Debug 3
}

The first print (Debug 1) will print a line similar to des313,3,,,.

The second print (Debug 2) will print a line DATA: des313,3

The third print (Debug 3) will print a blank line.

The issue seems to be in the way I am trying to insert the data into the associative array. I have tried both the direct insert and the push method with no results. I have done this with PHP however I think I am overlooking this in Perl. I did look at the perldoc perldsc page in the section of HASHES of HASHES however I did not see it talk about dynamic generation of them. Any suggestions would be great!

  • 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-28T03:54:14+00:00Added an answer on May 28, 2026 at 3:54 am

    Assigning to the hash the way you have should work fine. You are declaring your variables improperly. Your associative array is called a hash in Perl, and is prefixed with a % sigil, so you should write my %data before the while loop. Inside the loop, the my operator needs parens to apply to a list, so it should be my ($ID, $COUNT);.

    This minimal example works properly:

    use warnings;  # place these lines at the top of all of your programs
    use strict;    # they will catch many errors for you
    
    my %data;  # hash variable
    while (<DATA>) {
        chomp;
        my ($id, $count) = split /,/;  # simplify the split
    
        $data{$id}{count} = $count;    # build your hash
    }
    
    print "got: $data{des313}{count}\n";  # prints "got: 3"
    
    __DATA__
    des313,3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a simple fix - but I'm trying to sum together all
This may be a simple answer, but I'm trying to add a dom-created element
New to Grails and this may be simple problem. I am trying to save
this may sound too simple, but I'm missing something. I need to write a
This may be simple one, but 5 mins of Googling didn't give me the
This may well be simple, but I'm new to Drupal. The organization I work
This may be a simple question though can´t figure out how to do it.
This may be very simple question,But please help me. i wanted to know what
Okay this may be a simple question but I have yet to come with
Okay, This may be really simple or it may not even be possible, or

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.