Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My first go at dealing with delimited data has not been as straightforward as I expected :(

After stuffing around with various Text/CSV modules and super searching all over the place for an hour or two, I finally gave up and decided to write my own parsing routine.

The data is tab delimited, with the first line being a header.
Each column represents user attributes, and each row represents the data for a particular user.
The only inconsistency in the data (if it is that), is that the header row has a leading hash (#), whereas the rest of the data doesn't.

All I wanted to do is build a hash from the data, in the form $data->{$user}{$attribute}

Although the code I have works, and is only about a dozen lines, I'm disappointed - because:

a) I'm sure this could be done easier with the appropriate module - but I'm damned if I could work out how, and
b) I've used both a flag and a counter, which I'm sure are redundant - but I don't know how to get rid of them

Any advice?
Thanks in advance
--Darren
#!/usr/bin/perl -w use strict; my $datafile = "stuff.csv"; my @fields; my $data; my $flag; open DATA, "<$datafile" or die "Could not open $datafile:$!\n"; while (<DATA>) { chomp(); # If the flag hasn't been set, this must be the header (first) lin +e if (!$flag) { # Throw away the first two fields (#, username) in the header (undef, undef, @fields) = (split /\t/); $flag++; } else { my ($user, @userdata) = (split /\t/); my $i = 0; while ($i <= $#fields) { $data->{$user}{$fields[$i]} = $userdata[$i]; $i++; } } } close DATA;

In reply to Parsing CSV into a hash by McDarren

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-29 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found