Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^7: Read CSV with column mapping

by poj (Abbot)
on Dec 16, 2018 at 18:17 UTC ( #1227322=note: print w/replies, xml ) Need Help??


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

You could just check if the key is blank

#!/usr/bin/perl 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 $key.':'; print join ',', @{$hash_ref->{$key}}; print "\n"; } sub csv_file_hashref { my ($filename) = @_; my %output_hash = (); my $csv_fh = IO::File->new($filename, 'r'); my $csv = Text::CSV_XS->new( { allow_whitespace => 1 } ); while (my $row = $csv->getline($csv_fh)){ my $key = shift @$row; next if $key =~ /^#/; if ($key =~ /\S/){ $output_hash{$key} = $row ; } } return \%output_hash; }

Note $item =~ /^\s+$/ matches 1 or more white space only (but not nothing). Try $item =~ /^\s*$/ in your code.

poj

Replies are listed 'Best First'.
Re^8: Read CSV with column mapping
by coretele (Novice) on Dec 17, 2018 at 21:43 UTC
    Thanks. This worked for me now. On the side note. How can I implement object oriented concept (encapsulation) in this same scenario? e.g. if I want to do following. A.pm
    -> sub Initialize read config file and construct hash and return self object sub get_value(string parameter, string key) return value
    config file:
    parameter, key, value, id abc, xyz, 1, 100 blue, pqr, 0.1, 101
    Thanks.

        Thank you! I made little change in the code as per our code standard and it worked perfectly fine. Thank you once again!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2019-11-17 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (86 votes). Check out past polls.

    Notices?