Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: csv to hash table

by 2teez (Vicar)
on Nov 19, 2013 at 05:01 UTC ( #1063267=note: print w/replies, xml ) Need Help??

in reply to csv to hash table

Hi waytoperl,
..I'm getting error..
What error or (type) are you getting?
Use either Text::CSV or Text::CSV_XS for your CSV file, instead of split function. Then the rest is just manipulation of dataset.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: csv to hash table
by waytoperl (Beadle) on Nov 19, 2013 at 05:18 UTC

    perl readline() on unopened filehandle at line xx (#1) (W unopened) An I/O operation was attempted on a filehandle that was never initialized. You need to do an open(), a sysopen(), or a socket() call, or call a constructor from the FileHandle package. GLOB(0x5fc7210)ARRAY(0x603a130)

    line xx: is $hash{$keyfile1} = \@arrayfile1;

      You're confusing the filehandle with the record being read.

      Using more code than is really necessary to demonstrate the point:

      open my $filehandle, ... while (my $record = <$filehandle>) { chomp $record; # Remove the newline # Do stuff with the record text, e.g. my @fields = split /$field_separator/ => $record; ... }

      That would normally be coded more succinctly as:

      open my $filehandle, ... while (<$filehandle>) { chomp; # Remove the newline # Do stuff with the record text, e.g. my @fields = split /$field_separator/; ... }

      In the second code fragment, all instances of $record (from the first fragment) are now $_, which (being the default for chomp, split, and many other functions) can be omitted.

      And, just in case it wasn't obvious, that was for general enlightenment, not a recommendation to attempt your own version of Text::CSV. :-)

      -- Ken

        Why => in split /$field_separator/ => $record? To my eye that says paired item, but there it's just a plain ol' comma and would be clearer as:

        my @fields = split /$field_separator/, $record;

        Personally I prefer making the record variable explicit, but I'd write the while expression as:

        while (defined (my $record = <$filehandle>)) {

        which is a closer match to the magic you get with while (<$filehandle>) {.

        True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063267]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-03-24 06:30 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (297 votes). Check out past polls.