Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an entire file into a hash in Perl

Tags:

hash

perl

I have some problems reading a file into a hash in Perl.

Chr1_supercontig_000000000  1   500
    PILOT21_588_1_3_14602_59349_1
Chr1_supercontig_000000001  5   100
    PILOT21_588_1_21_7318_90709_1
    PILOT21_588_1_43_18803_144592_1
    PILOT21_588_1_67_13829_193943_1
    PILOT21_588_1_42_19678_132419_1
    PILOT21_588_1_67_4757_125247_1
...

So I have this file above. My desired output is a hash with the "Chr1"-lines as key, and the "PILOT"-lines as values.

Chr1_supercontig_000000000 => PILOT21_588_1_3_14602_59349_1
Chr1_supercontig_000000001 => PILOT21_588_1_21_7318_90709_1, PILOT21_588_1_43_18803_144592_1,...

As far as I know, multiple values can be assigned to a key only by reference, is that correct?

I got stuck at this point and need help.

like image 625
Philipp Avatar asked Dec 05 '22 20:12

Philipp


2 Answers

You are right, the hash values need to be references that point to arrays which contain the PILOT lines.

Here's a way to do it:

my %hash;
open FILE, "filename.txt" or die $!;
my $key;
while (my $line = <FILE>) {
     chomp($line);
     if ($line !~ /^\s/) {
        ($key) = $line =~ /^\S+/g;
        $hash{$key} = [];
     } else {
        $line =~ s/^\s+//;
        push @{ $hash{$key} }, $line;
     }
 }
 close FILE;
like image 143
alexk Avatar answered Dec 11 '22 16:12

alexk


You can read the file line-by-line keeping track of the current hash key:

open my $fh, '<', 'file' or die $!;

my (%hash, $current_key);

while (<$fh>) {
    chomp;        
    $current_key = $1, next if /^(\S+)/;
    s/^\s+//; # remove leading space
    push @{ $hash{$current_key} }, $_;
}
like image 38
Eugene Yarmash Avatar answered Dec 11 '22 14:12

Eugene Yarmash