So, I've read over
explanations on making a hash of arrays and attempted to adopt the methods into a program to take a simple datafile and turn it into a hash of arrays. The file has the format
1,2
1,3
1,4
2,3
2,5
2,5
And I want to make a hash of arrays where the first column is the key and the second column is the data. I've done it like so:
local %DATAHASH;
open (DATA, 'foo.csv');
while (<DATA>){
chomp;
my @datarow=split(/,/, $_);
push(@{$DATAHASH{$datarow[0]}}, ($datarow[1]));
}
And yet, whenever I try and print out the contents of DATAHASH, either after the while loop or in its midst using Dumper($DATAHASH}, I get a $VAR1 = undef
Any advice on remedying this situation?
And looking further down the line, for something that will have two seperate keys (e.g.
1,2,3 is a line from the file) I'm using
push( @{$DATAHASH{$datarow[0]}->{$datarow[1]}}, ($datarow[2]) );, which I'm assuming I'll just modify in the same way.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.