Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Making a hash of arrays from a datafile by redbeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2021-05-06 01:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Perl 7 will be out ...





      Results (69 votes). Check out past polls.

      Notices?