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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.