Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
I have a file with data like this:

id_1 info1
id_2 info2
id_3 info3

id_1 info1
id_2 info2
id_3 info3
and all I want to do is use a hash to put this into arrays eg: id_1 all the info with this id here...
so id is my key.
Here's the code:
while (<MYFILE>) { { next if /^\s*CLUSTAL/; #remove line starting with CLUSTAL + heading next if /:/; #skip the consensus lines which contain : +and * next if /\*/; next if /^ /; my($id, $seq) = split; #splits each line to +id and sequence $newid = substr ($id, 0, 9); #remove all but 9 characters from +id #so the id's are all the same length push @{$species{$newid}}, $seq; #each time adds +sequence to #a unique id and puts sequence in an + array (hash is %species) } } while (($key, $sequence)=each %species) { print DATA ">", $key, " ", join " ", @$sequence; #\t to sep +arate with tab print DATA "\n"; } close(MYFILE); close(DATA);
the problem is, I seem to get a blank record printed out at
the top of the page - I am adding the symbol > to the front of each id

what I get in my DATA file is

>
>id1 info
>id2 info

any idea why I get this blank record printed first?
thanks

Replies are listed 'Best First'.
Re: hash printing - why a blank record ?
by particle (Vicar) on Jun 12, 2002 at 11:42 UTC
    if you have blank lines in your data, you're not skipping them properly. try

    next if /^\s*$/;
    p.s. you can use Data::Dumper to see how your hash is being built.

    use Data::Dumper; ## ... print Dumper \%species;

    ~Particle *accelerates*