BlueStarry BlueStarry - 5 months ago 23
Perl Question

Double hash entries while printing

As part of a bigger program I've got a hash. I'm testing the program printing the keys but they are all duplicates, I don't know why

while ( my $line = <SEQ> ) {

chomp $line;
$line =~ s/>//;

my ( @split1 ) = split( "\t", $line );

foreach my $chr ( keys %position ) {

#print Dumper \%position;
print "$chr\n";

foreach my $pos ( sort keys %{ $position{$chr} } ) {

if ( $split1[0] =~ /$chr/ ) {
#print "$chr\t$pos\n";
}
}
}
}


%position
is a nested hash, when I print the keys on
print "$chr\n";
they are all doubled and I don't understand why.

The file opened on handle
SEQ
looks like this:

>chr1\tACTGTAGTCTCATCCTAT...
>chr2\tACGTAGCTAGT....


and so on

Answer

Start with this:

my %data;
while(<DATA>){
    chomp;
    my ($chr, $seq) = split;
    $data{$chr} = $seq;
}

print Dumper \%data;