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

Re: Help with pushing into a hash

by CountZero (Bishop)
on Aug 29, 2012 at 19:56 UTC ( #990548=note: print w/replies, xml ) Need Help??

in reply to Help with pushing into a hash

I did it so:
use Modern::Perl; use Data::Dump qw/dump/; my %acn_database; while (<DATA>) { last if /END/; my ($acn, $orfname) = split /\sORFNames=|\s/; $acn_database{$acn} = [$orfname]; } while (<DATA>) { my ($acn, @pf_data) = split /\s\|\s|\s/; push @{$acn_database{$acn}}, @pf_data; } say dump(\%acn_database); __DATA__ Q197F8 ORFNames=IIV3-002R Q197F7 ORFNames=IIV3-003L Q6GZX2 ORFNames=FV3-003R Q6GZX1 ORFNames=FV3-004R Q197F5 ORFNames=IIV3-005L Q6GZX0 ORFNames=FV3-005R ;PF02393 Q91G88 ORFNames=IIV6-006L ;PF12299;PF04383 Q6GZW9 ORFNames=FV3-006R END Q197F8 | PF04947.9 Q91G88 | PF01486.12 PF00319.13
I have assumed that the stray space at the first line of your first file is just a typo.

Note that this will still do the right thing if you have multiple lines in your second file with the same accession numbers: their data will just be added to right array in the hash.


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990548]
[LanX]: what?
[LanX]: $; exists, so does the glob
[GotToBTru]: $; is "useful for emulating multi-dimensional arrays in Perl4"

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-01-18 21:02 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (164 votes). Check out past polls.