Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Read CSV with column mapping

by coretele (Novice)
on Nov 25, 2018 at 07:31 UTC ( #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 (Pope) 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
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? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2019-11-20 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (97 votes). Check out past polls.

    Notices?