in reply to Re^2: how to compare two hashes with perl?
in thread how to compare two hashes with perl?

I hear hash is random when it prints output

That just means that the order in which you add key/value pairs to a hash is not the order in which they are stored in the hash. Here is an example:

use strict; use warnings; $\ = "\n"; $, = ', '; my %hash = (); $hash{"h"} = 10; $hash{"z"} = 20; $hash{"a"} = 30; foreach my $key (keys %hash) { print "$key: $hash{$key}"; } --output:-- a: 30 h: 10 z: 20

However, the key/value pairs are the same. A key will never be associated with a value that you did not enter for that key.

it's just confusing and I'm not exactly sure how my file gets stored in hash

Take a look at this example:

use strict; use warnings; $\ = "\n"; $, = ', '; my %results = (); my $line = 'HWUSI-EAS548:7:1:5:1527#0/1 + chr12 52084152 CGGAGC'; my @pieces = split /\s+/, $line; my $id = $pieces[0]; my $seq = $pieces[-1]; $results{$id} = $seq; foreach my $key (keys %results) { print "$key -----> $results{$key}"; } --output:-- HWUSI-EAS548:7:1:5:1527#0/1 -----> CGGAGC

If you want to gather all the sequences corresponding to an id, you can do this:

use strict; use warnings; $\ = "\n"; $, = ', '; my %results = (); while (<DATA>) { my @pieces = split /\s+/; my $id = $pieces[0]; my $seq = $pieces[-1]; $results{$id} = [] unless exists $results{$id}; push @{$results{$id}}, $seq; } foreach my $key (keys %results) { my $arr_str = join ',', @{$results{$key}}; print "$key -----> [$arr_str]"; } __DATA__ HWUSI-EAS548:7:1:5:1527#0/1 + chr12 52084152 CGGAGC HWUSI-EAS548:7:1:5:1527#0/1 + chr12 52084152 XXXXXX Some_other_id + chr12 52084152 CGGAGC

You might want to experiment a little more with hashes in a separate practice program. For instance, you might want to read perlintro and perldsc, which you can read by typing:

$ man perlintro or $ man perdsc

For a complete list of topics available type:

$man perl

and scroll down.

Replies are listed 'Best First'.
Re^4: how to compare two hashes with perl?
by 7stud (Deacon) on Nov 04, 2009 at 23:36 UTC
    while (<DATA>) { my @pieces = split /\s+/; my $id = $pieces[0]; my $seq = $pieces[-1]; $results{$id} = [] unless exists $results{$id}; push @{$results{$id}}, $seq; }

    Actually, as perlreftut instructs, the line:

    $results{$id} = [] unless exists $results{$id};

    is unnecessary. I highly recommend that you read perlreftut:

    $ man perlreftut