Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Parsing CSV into a hash

by GrandFather (Sage)
on Sep 19, 2005 at 21:53 UTC ( #493312=note: print w/replies, xml ) Need Help??


in reply to Parsing CSV into a hash

You might like to replace you inner while loop with:

$data->{$user}{$_} = shift @userdata for (@fields);

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Parsing CSV into a hash
by McDarren (Abbot) on Sep 19, 2005 at 22:05 UTC
    Okay, that's one part of my problem solved, thanks :)

    Let me just work through that to make sure I have it....
    It works from right to left, yes?
    We iterate through each item in @fields, grabbing the associated item from @userdata, then assigning it to the hash, yes?

      That's right. Note also that if there are too few fields in a line this simply assigns undef rather than generating an invalid access as your original code did.

      If you are sure that the first line contains the header line then you can simply:

      $_ = <DATA> or die "Empty file"; chomp; # Throw away the first two fields (#, username) in the header (undef, undef, @fields) = (split /\t/);

      before the loop.


      Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://493312]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-10-18 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (106 votes). Check out past polls.

    Notices?