I am working on an assignment and I am having some issues getting portions of a certain file to be printed to the output file in a hash. I was given a large file containing a list of different species (along with some other variables that aren't too important) and I am getting stuck on how to isolate that specific column and put it into a hash that can be printed to the output while counting how many times each species is mentioned.
#!/usr/bin/perl
use strict; use warnings;
open IN, $ARGV[0]; ## open input file given as argument 1
open OUT, ">", $ARGV[1]; ## open output file given as argument 2
my @cols; ## creates variable to hold column of data
print "\nWorking on file: $ARGV[0]\n\n"; ## while data exists in input file, read
## line by line
while (my $file = <IN>) {
chomp $file; ## remove trailing newline
print "$file\n";
@cols = split /\t/, $file; ## split data into columns on tab
print "@cols[9]\n";
my %hits;
$hits{species} += 1;
print "$hits{species}\n";
print OUT "@cols[9]\n"; ##write species column to output file
}
print "File has been read and output written!\n";
close IN;
close OUT;
This is currently what I have for my code and any suggestions or tips would be greatly appreciated. Thanks!
Sample of input data (Bacteria_firmicutes is the 11th column)
Query dbj|BAI87270.2| 1 456 98.048 461 911 0.0 645657 Bacillus_subtilis Bacteria_firmicutes