Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Do you know where your variables are?
 
PerlMonks  

Re: csv to hash table

by 2teez (Priest)
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


Comment on Re: csv to hash table
Re^2: csv to hash table
by waytoperl (Acolyte) on Nov 19, 2013 at 05:18 UTC

    perl perl_program.pl readline() on unopened filehandle at perl_program.pl 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-04-20 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (486 votes), past polls