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 /,/, ; chomp @fields; while () { chomp; my %row; @row{@fields} = split /,/; my @wanted_data = map {$row{$_}} @wanted_fields; print join(",", @wanted_data), "\n"; }