Input:
NAME Age Occupation Place
X1 43 Artist Italy
X2 42 Artist Germany
Y1 56 Artist France
I need to extract the NAME and age column.
My code:
#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils;
my $file = @ARGV[0];
open(FH, "< $file") or die "Cannot open $file for reading: $!";
my @array = <FH>;
close FH or die "Could not open file: $!";
open(OUT, ">$file") or die "Cannot open $file for write access: $!";
print OUT splice(@array,4);
close OUT or die "Could not close file: $!";
open(MYFILE,"< $file") or die "Cannot open $file for read access: $!";
open(my $OFILE, '>Output.txt') or die "Cannot create file for output: $!";
my @wanted = ("NAME","AGE");
my @output = qw/NAME AGE/;
my @fields = split /\t/, <MYFILE>;
chomp @fields;
print $OFILE join("\t",@output), "\n";
while(<MYFILE>)
{
chomp;
my %row;
@row{@fields} = split /\t/;
my @wanted_data = map{$row{$_}} @wanted;
print $OFILE join("\t", @wanted_data), "\n";
}
close $OFILE or die "Error closing $OFILE: $!";
i am getting error like Use of uninitialized value in join or string at
print $OFILE join("\t", @wanted_data), "\n";
So the header alone got print in my output.txt
Thanks, N.
@row{@fields} = split /\t/;. You don't useList::MoreUtilsAFAICS.