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++; }
In reply to Printing particular column is not working by kumarkeyan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |