- Why do you read into @data and the assign to new fields for every line??
- Why do you use slices in reading @data (@data[1] better written as $data[1], which would have been prompted to you when you would have used strict and warnings)
- Why do you quote scalars? (no need to put "'s around $Symbols)
Read ahead and see if that makes some sense:
use 5.16.2;
use warnings;
my $annotationfile = "file.tsv";
open my $fh, "<", $annotationfile or die "$annotationfile: $!\n";
# First read the header
my @hdr = split m/\t/ => scalar <$fh>;
my %GOHash;
# Now read every line
while (<$fh>) {
chomp;
# read as a hash
my %hash;
@hash{@hdr} = split m/\t/ => $_, 11;
$GOHash{$hash{Symbol}}{$hash{"Taxon Name"}} = $hash{ID};
}
FWIW you can read the whole file in one single statement into an array of hashes using recent Text::CSV_XS:
use 5.16.2;
use warnings;
use Text::CSV_XS qw( csv );
my $AoH = csv (in => "file.tsv", sep_char => "\t", headers => "auto");
foreach my $row (@$AoH) {
print $row->{"GO ID"}, $row->{ID};
}
Enjoy, Have FUN! H.Merijn
scalar
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.