kumarkeyan has asked for the wisdom of the Perl Monks concerning the following question:
The above code is working fine if I give the field name in the header array(@header). But when I try to get the header value from file and assign that to @header, is not working properly.my @header=qw/studentrecord primary_ins primary_ins_type/; my @wanted_fields = @header; open(DATA, "sample_1.csv") or die "Could not open file $!"; my @fields = split /,/, <DATA>; chomp @fields; while (<DATA>) { chomp; my %row; @row{@fields} = split /,/; my @wanted_data = map {$row{$_}} @wanted_fields; print join(",", @wanted_data), "\n"; }
And throws the below error. Use of uninitialized value $wanted_data[0] in join or string at samp_1.pl line 39, <DATA> line 98. Let me know your thoughts on this.open (FH, "< sample_1_header.csv") or die "Can't open file for read: $ +!"; my @header=<FH>; my $i=0; foreach(@header) { my @array=$header[$i]; print @array; my @wanted_fields = @array; open(DATA, "sample_1.csv") or die "Could not open file $!"; my @fields = split /,/, <DATA>; chomp @fields; #print Dumper \@fields; while (<DATA>) { chomp; my %row; @row{@fields} = split /,/; my @wanted_data = map {$row{$_}} @wanted_fields; print join(",", @wanted_data), "\n"; } $i++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing particular column is not working
by dasgar (Priest) on Jul 23, 2014 at 15:56 UTC | |
|
Re: Printing particular column is not working
by AppleFritter (Vicar) on Jul 23, 2014 at 16:46 UTC | |
|
Re: Printing particular column is not working
by fishmonger (Chaplain) on Jul 24, 2014 at 13:39 UTC | |
|
Re: Printing particular column is not working
by tbone654 (Beadle) on Jul 24, 2014 at 13:53 UTC | |
by fishmonger (Chaplain) on Jul 24, 2014 at 14:02 UTC |