Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Multidimensional hash help!

by Tux (Monsignor)
on Mar 04, 2014 at 12:22 UTC ( #1076863=note: print w/ replies, xml ) Need Help??


in reply to Multidimensional hash help!

  • 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


Comment on Re: Multidimensional hash help!
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-10-02 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls