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 | |
by poj (Abbot) on Dec 18, 2018 at 19:45 UTC | |
by coretele (Novice) on Dec 25, 2018 at 04:47 UTC |
In Section
Seekers of Perl Wisdom