Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Read CSV with column mapping

by Corion (Patriarch)
on Dec 14, 2018 at 22:44 UTC ( [id://1227278]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Read CSV with column mapping
in thread Read CSV with column mapping

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?

Replies are listed 'Best First'.
Re^4: Read CSV with column mapping
by coretele (Novice) on Dec 16, 2018 at 02:11 UTC

    I have tried adding if conditions as shown in following code but it is not skipping to add comment line and empty line. Am I doing something wrong?

    while(my $colref = $csv->getline ($csv_fh)) { if (scalar(@$colref) =~ /^#/) { last; } if (scalar(@$colref) =~ /^s+$/) { last; } $output_hash{shift @{$colref}} = $colref; }
    config file
    #ksjlasjda abc xyz,10 xyz pqr,2 pqr stq,0.1

      scalar(@$colref) is the number of items found in the column, it would never contain a '#' symbol.

      $colref->[0] is the first item on the line read in, it could contain a # symbol.

      if ($colref->[0] =~ /^#/) { next; }

      Notice i said next rather than last. next will go on to the next row, last will end the do loop and stop reading any more lines.

      the next condition is a touch more tricky.

      my $anynonblank=0; for my $item (@$colref){ unless ($item =~ /^\s+$/) { $anynonblank=1; last; } } unless ($anynonblank) { next; }
      See we have to test all of the items in this case. Note th use of last here, as soon as we have found any nonblank we dont have to check anymore.

      edit: opps , fixed as per Re^6: Read CSV with column mapping below

        unless ($item =~ /^s+$/) { ... }

        The  $item =~ /^s+$/ expression matches against a  's' literal character and not, as I think was intended, the  \s whitespace character class.


        Give a man a fish:  <%-{-{-{-<

        I have tried your suggestion and it did help to avoid comments but not for blank lines. Here I am provide the code and config file. Also providing output.

        use strict; use warnings; use Data::Dumper; use Text::CSV_XS; use IO::File; my $hash_ref = csv_file_hashref('parameters.conf'); foreach my $key (sort keys %{$hash_ref}){ print qq{$key:}; print join q{,}, @{$hash_ref->{$key}}; print qq{\n}; } 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)) { if ($colref->[0] =~ /^#/) { next; } my $anynonblank=0; for my $item (@$colref){ unless ($item =~ /^\s+$/) { $anynonblank=1; last; } } unless ($anynonblank) { next; } #print @$colref; #print "\n"; $output_hash{shift @{$colref}} = $colref; } return \%output_hash; } 1;
        config file-
        #sdadwlasda abc xyz,10 #ldfgld xyz pqr,2 #jwej pqr stq,0.1 # # # #
        Output-
        : abc xyz:10 pqr stq:0.1 xyz pqr:2

Log In?
Username:
Password:

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

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

    No recent polls found