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!
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 writemy %databefore the while loop. Inside the loop, themyoperator needs parens to apply to a list, so it should bemy ($ID, $COUNT);.This minimal example works properly: