Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to set variable names using Text::CSV?

by Tux (Canon)
on Oct 16, 2009 at 06:13 UTC ( [id://801502]=note: print w/replies, xml ) Need Help??


in reply to How to set variable names using Text::CSV?

I will only comment on the flaws in script 1, as that deals with Text::CSV

  • my @names = $csv->getline ($io); assigns an array ref to a list. That's not what you want.
    my @names = @{$csv->getline ($io)}; is the coreect mantra.
  • always_quote is void on parsing. That is a generator option.
  • make it a habit to always pass binary => 1
  • Use auto_diag => 1 on csv initiation or $csv->oef or $csv->error_diag () after parsing
use strict; use warnings; use Text::CSV; use Data::Dumper; open my $io, "<", "player_characters.csv" or die "player_characters.cs +v: $!"; my $csv = Text::CSV_XS->new ({ sep_char => "|", allow_whitespace => 1, blank_is_undef => 1, binary => 1, auto_diag => 1, }); my @names = @{$csv->getline ($io)}; $csv->column_names (@names); while (defined (my $hr = $csv->getline_hr ($io))) { print Dumper ($hr); }

If you do not use the @names after you set it, other than to assign to to column names, you can combine the in one call, as column_names () accepts both a list or a list ref

$csv->column_names ($csv->getline ($io));

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: How to set variable names using Text::CSV?
by Lady_Aleena (Priest) on Oct 16, 2009 at 06:32 UTC
    Tux,

    Why should I always pass binary => 1 when I am dealing with ASCII characters? I don't know what auto_diag => 1 does, so I would prefer not to use it. I also noticed that you changed the || to or, would you please tell me why? It also looks like @names keeps the order that I want, so I think I will keep it.

    Have a nice day!
    Lady Aleena

      IMHO binary should have been the default when the module was created, but that was before I took over. Now we have to keep backward compatibility. There are two reasons to always pass binary

      1. You don't forget the attribute syntax (people still get that wrong)
      2. You never know when your-all-ASCII suddenly uses binary characters

      Of course, when it is your own data, and you generate it yourself, feel free to leave it out, but it is the most given solution I have to give to people that sen me mail with "Text::CSV_XS is broken" subjects.

      auto_diag is a rather new attribute that calls error_diag () at the moment the error occurs, so you will (hopefully) never wonder why something went wrong.

      There is no difference between

      my @names = @{$csv->getline ($io)}; # note the @{...} $csv->column_names (@names);

      and

      my @names = @{$csv->getline ($io)}; # note the @{...} $csv->column_names ( [ @names ] ); # extra whitespace for attention

      which is essentially the same as

      $csv->column_names ($csv->getline ($io));

      when you do not intend to use @names after the column assignment. The order is the same for all. If you need the headers later, you can still use

      my @names = $csv->column_names ();


      Enjoy, Have FUN! H.Merijn
        Why not create a new package with modern defaults?
        use Text::CSV; my $t = Text::CSV::B->new; # binary 1 by default ....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found