Perl脚本中的HASH(0x1970c80)

时间:2014-08-05 15:38:23

标签: perl hash

我有以下代码:

#!/usr/bin/perl

use warnings;
use strict;

my $count;
my @chomp;
my $length;
my %hash;
my $orf;

open FILE, "<", $ARGV[0];

while ( my $line = <FILE> ) {
    if ( $line =~ /LOCUS/ ) {
        $count++;
        $line =~ s/ +/\t/g;
        @chomp                  = split( /\t/, $line );
        $length                 = $chomp[2];
        $hash{$count}->{length} = $length;
    }
    elsif ( $line =~ /misc_feature    (\w+)\.\.(\w+)/ ) {
        $orf = $2 - $1;
        if ( !defined $hash{$count}->{orf} or $hash{$count}->{orf} < $orf ) {
            $hash{$count}->{orf} = $orf;
        }
    }
}

for my $key (%hash) {
    print $key. "\n";

    #       print $hash{$key}->{"orf"}."\t".$hash{$key}->{"length"}."\n";
}

提供以下输出:

HASH(0x140ae60)
13891
HASH(0x18d4060)
5056
HASH(0x15c4968)
15612
HASH(0x1970c80)
18787
HASH(0x1a98448)
7684

我不明白为什么会这样?它应该打印$count值(例如1,2,3 ......)。事实上,它是打印的,但那里有HASH(0x

5 个答案:

答案 0 :(得分:2)

你应该使用

for my $key (keys %hash)
{ ... }

答案 1 :(得分:1)

你需要尊重,因为它是哈希的散列:

use warnings;
use strict;

for my $length (keys %hash) {
        print "$length\n";
            for my $count (keys %{$hash{$length}}) {
                print "$count\n";
            }
}

答案 2 :(得分:0)

那是因为你正在建立一个散列哈希:

$hash{$count}->{length} = $length;

当您打印$hash{whatever}时,您正试图打印出内部&#34;内部&#34; hash,这是你HASH(0xXXXXXXX)输出的来源。

答案 3 :(得分:0)

for my $key (keys %hash) {
        print $key."\n";
#       print $hash{$key}->{"orf"}."\t".$hash{$key}->{"length"}."\n";
}

答案 4 :(得分:0)

正如其他许多人所说,如果您想获得密钥,则需要使用keys。所以:

for my $key (keys %hash)
             ^^^^
             here

完整的例子:

use strict;
use warnings;
use feature 'say';

use Data::Dumper;
my %hash = (
    cnt13 => {
        'length' => 'len13'
    },
    'cnt12' => {
        'orf' => 'orf12',
        'length' => 'len12'
    },
    'cnt11' => {
        'length' => 'len11'
    },
    'cnt10' => {
        'orf' => 'orf10',
        'length' => 'len10'
    }
);

for my $key (keys %hash) {
    say "key is CNT: $key";
    for my $subkey (keys %{$hash{$key}} ) {
        say "\tGot a subkey: $subkey with a value: $hash{$key}->{$subkey}";
    }
}

say Dumper \%hash;

打印:

key is CNT: cnt11
    Got a subkey: length with a value: len11
key is CNT: cnt13
    Got a subkey: length with a value: len13
key is CNT: cnt12
    Got a subkey: orf with a value: orf12
    Got a subkey: length with a value: len12
key is CNT: cnt10
    Got a subkey: length with a value: len10
    Got a subkey: orf with a value: orf10
$VAR1 = {
          'cnt11' => {
                       'length' => 'len11'
                     },
          'cnt13' => {
                       'length' => 'len13'
                     },
          'cnt12' => {
                       'orf' => 'orf12',
                       'length' => 'len12'
                     },
          'cnt10' => {
                       'length' => 'len10',
                       'orf' => 'orf10'
                     }
        };