in reply to Re^2: Load a file into hash with duplicate keys
in thread Load a file into hash with duplicate keys

Show us the input file Table1.txt.
Here you are splitting the input line based on tab delimiter and collecting the first element[0] in  $key
my $key = (split/\t/, $line)[0];
Here you are pushing every line that has the same key into an array
push @{ $hash{$key} }, $line;
There can only be unique keys in such a data structure. UPDATE
while(<DATA>) { chomp; my $line = $_; my $key = (split/\s+/, $line)[0]; push @{ $hash{$key} }, $line; } print Dumper(\%hash); __DATA__ NM_001127328 202502_at NM_000018 200710_at NM_000019 205412_at NM_001111067 203935_at NM_000023 210632_s_at NM_000027 204332_s_at NM_000027 204333_s_at NM_000027 216064_s_at NM_000029 202834_at NM_000031 218487_at NM_000031 218489_s_at NM_000032 211560_s_at NM_000036 206121_at NM_000042 205216_s_at NM_000043 204780_s_at NM_000043 204781_s_at NM_000043 215719_x_at NM_000043 216252_x_at NM_000044 211110_s_at NM_000044 211621_at
prints...
$VAR1 = { 'NM_000032' => [ 'NM_000032 211560_s_at' ], 'NM_000044' => [ 'NM_000044 211110_s_at', 'NM_000044 211621_at' ], 'NM_001111067' => [ 'NM_001111067 203935_at' ], 'NM_000023' => [ 'NM_000023 210632_s_at' ], 'NM_001127328' => [ 'NM_001127328 202502_at' ], 'NM_000031' => [ 'NM_000031 218487_at', 'NM_000031 218489_s_at' ], 'NM_000043' => [ 'NM_000043 204780_s_at', 'NM_000043 204781_s_at', 'NM_000043 215719_x_at', 'NM_000043 216252_x_at' ], 'NM_000029' => [ 'NM_000029 202834_at' ], 'NM_000042' => [ 'NM_000042 205216_s_at' ], 'NM_000019' => [ 'NM_000019 205412_at' ], 'NM_000018' => [ 'NM_000018 200710_at' ], 'NM_000027' => [ 'NM_000027 204332_s_at', 'NM_000027 204333_s_at', 'NM_000027 216064_s_at' ], 'NM_000036' => [ 'NM_000036 206121_at' ] };

Replies are listed 'Best First'.
Re^4: Load a file into hash with duplicate keys
by sophix (Sexton) on Nov 02, 2010 at 20:54 UTC

    As AnonymousMonk suggested, I used the second colum as the key.

    #!/usr/bin/perl -w use Data::Dumper; my $data = '/Data/Table1.txt'; open INFILE, '<', $data or die "aa\n"; my %hash = map { chomp; split /\t/ } <INFILE>; print Dumper(\%hash); my $data2 = '/Data/Table2.txt'; open INFILE2, '<', $data2 or die "aa\n"; my %hash2; while(<INFILE2>) { chomp; my $line = $_; my $key = (split/\t/, $line)[0]; push @{ $hash2{$key} }, $line; } print Dumper(\%hash2);

    This returns the following output

    $VAR1 = { '218489_s_at' => 'NM_000031', '216064_s_at' => 'NM_000027', '218487_at' => 'NM_000031', '211110_s_at' => 'NM_000044', '204333_s_at' => 'NM_000027', '210632_s_at' => 'NM_000023', '203935_at' => 'NM_001111067', '211560_s_at' => 'NM_000032', '204332_s_at' => 'NM_000027', '215719_x_at' => 'NM_000043', '204780_s_at' => 'NM_000043', '202834_at' => 'NM_000029', '206121_at' => 'NM_000036', '204781_s_at' => 'NM_000043', '205412_at' => 'NM_000019', '216252_x_at' => 'NM_000043', '205216_s_at' => 'NM_000042', '211621_at' => 'NM_000044', '200710_at' => 'NM_000018', '202502_at' => 'NM_001127328' }; $VAR1 = { '211560_s_at' => [ '211560_s_at P P P + A P P P' ], '204332_s_at' => [ '204332_s_at P P A + P P P P' ], '216064_s_at' => [ '216064_s_at P M P + P P A A' ], '200005_at' => [ '200005_at P P A P + P P A' ], '211110_s_at' => [ '211110_s_at P A P + A P A P' ], '200003_s_at' => [ '200003_s_at P A P + P P A P' ] };

    The first value elemant in the second hash includes the key.

      my $key = (split/\t/, $line)[0]; push @{ $hash2{$key} }, $line;
      Try this instead:

      my ($val, $key) = split /\t/, $line; push @{ $hash2{$key} }, $val;
        sorry, meant the other way round:

        my ($key, $val) = ...

        I think you meant this:

        my ($key, $val) = split /\t/, $line; push @{ $hash2{$key} }, $val;

        In this case, it only stores the first value.

        $VAR1 = { '211560_s_at' => [ 'P' ], '204332_s_at' => [ 'P' ], '216064_s_at' => [ 'P' ], '200005_at' => [ 'P' ], '211110_s_at' => [ 'P' ], '200003_s_at' => [ 'P' ] };