Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Read CSV with column mapping

by coretele (Novice)
on Nov 25, 2018 at 07:31 UTC ( [id://1226284]=perlquestion: print w/replies, xml ) Need Help??

coretele has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to perl and need your help on following problem. I have a csv file which has values like below and I want to write a perl module so other perl script can use the subroutine of that module.

csv: #header #Key,value,flag abc,0.10,1 xyz,5,1 pqr,0.01,0

perl module should have a subroutine to which parameter will be passed as "key" (column 1). e.g. get_value("abc"). Subroutine should read the CSV file and return "value" (column 2) corresponding to the key passed and also check the last column "flag" (column 3). If flag is 1 then it should return the value otherwise returns a message stating this is not active. Also I want to skip any of the comments in CSV file (starting with #) while reading it. Thanks in advance.

Replies are listed 'Best First'.
Re: Read CSV with column mapping
by Corion (Patriarch) on Nov 25, 2018 at 07:36 UTC

    Have you looked at Text::CSV_XS? It can read CSV files and convert them to hashrefs respecting the headers.

    Maybe when you have written the code, you can post the relevant parts here if you still need help.

      Thanks. Following I am providing ReadCSV.pm, parameters.csv and getvalue.pl. It is working fine but I want to avoid any comments (starts with #) in parameters.csv and also any blank lines. How can I do that in ReadCSV.pm ?

      ReadCSV.pm
      package ReadCSV; use strict; use warnings; use Data::Dumper; use Text::CSV_XS; use IO::File; # Implementation: sub csv_file_hashref { my ($filename) = @_; my $csv_fh = IO::File->new($filename, 'r'); my $csv = Text::CSV_XS->new (); my %output_hash; while(my $colref = $csv->getline ($csv_fh)) { $output_hash{shift @{$colref}} = $colref; } return \%output_hash; } 1;
      parameters.conf
      abc xyz,10 xyz pqr,2 pqr stq,0.1
      getvalue.pl
      use strict; use warnings; use Data::Dumper; use ReadCSV; my $hash_ref = ReadCSV::csv_file_hashref('parameters.conf'); foreach my $key (sort keys %{$hash_ref}){ print qq{$key:}; print join q{,}, @{$hash_ref->{$key}}; print qq{\n}; }

        In the loop where you read the CSV, only store the row if it is not a comment?

        Do the same for the blank lines?

Re: Read CSV with column mapping
by VinsWorldcom (Prior) on Nov 25, 2018 at 19:48 UTC

    Probably not the best example for learning, but this script does exactly what you want and a lot more:

    PESTS

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found