在Perl中解除引用多层哈希

时间:2014-07-31 13:38:26

标签: perl hash

我无法取消引用%spec_hash 它是多级哈希吗?

#!/perl/bin/perl
use FOOConf; #custom module 
use Data::Dumper ;
FOOConf::makeDBConnection(production);
use strict; 
use warnings; 

my $dbh=$EVTConf::dbh;
my $query = "select e_risk_symbol from gsd_etds where level_name='EXCH_CS' and e_exch_dest='XISX' and e_symbol_comment in ('Bin_6','Bin_56')";

if(!$dbh) {
    print "Error connecting to DataBase; $DBI::errstr\n";
    }  
    my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare statement:".$dbh->errstr;
    $cur_msg->execute();
    while (my @row=$cur_msg->fetchrow_array) {
           $spec_hash{'XISX'}{$row[0]}=1;
           }

    while(($key,$value) = each(%spech_hash)) {
        print $key. "," .$value ;
        }

我可以看到哈希的内容:

#!/perl/bin/perl
use strict; 
use warnings ; 
use FOOConf; # custom module we use for db access. 
FOOConf::makeDBConnection(production); # amkes a database connection. 
my $dbh=$EVTConf::dbh;
my $query = "select e_risk_symbol from gsd_etds where level_name='EXCH_CS' and e_exch_dest='XISX' and e_symbol_comment in ('Bin_6','Bin_56')";

if(!$dbh) {
    print "Error connecting to DataBase; $DBI::errstr\n";
    }
    my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare statement:".$dbh->errstr;
    $cur_msg->execute();
    while (my @row=$cur_msg->fetchrow_array) {
          foreach $row(@row) {
          print "$row ";
          }
    }

     print "\n";

这就是我得到的;

MTG GPS WM JBL ISIL MBI BA ILMN FCEL NDAQ CMS HOLX
INTC CYBX STLD MDT CTSH ASBC AMP KLAC LXK X MON 
SYY HIG UNM AMGN STZ KMP SONC ECA BEBE EAT PLCE 
SPN LAMR PDCO XLP GME CSGP EXC BHP

我可以通过浏览来查看加载到$ spec_hash的内容 @row数组,我可以转储%spec_hash

    while (my @row=$cur_msg->fetchrow_array) {
           $spec_hash{'XISX'}{$row[0]}=1;
           }

           print Dumper(%spec_hash)

在转储中是XISX哈希的名称和FCEL哈希XISX中的密钥和密钥FCEL的值是1

foo@fooserver:/tmp/walt $ ./just_db.row.dumper
$VAR1 = 'XISX';
$VAR2 = {
          'FCEL' => 1,
          'GPS' => 1,  
          'MCO' => 1,
          'DPZ' => 1,
          'WM' => 1,
          'SPLS' => 1,
          'ILMN' => 1,
          'BWLD' => 1,
          'CTSH' => 1,
          'EWU' => 1,
          'MDVN' => 1,
          'PDCO' => 1,
          'AFAM' => 1,
          'SHW' => 1,

我无法取消引用

 "$spec_hash{'XISX'}{$row[0]}=1;" 

我不明白这个哈希的加载方式。

1 个答案:

答案 0 :(得分:2)

执行此转储以查看更好的哈希结构:

print Dumper(\%spec_hash);

要取消引用你的需要:

 while(my($key,$value) = each(%{$spech_hash{'XISX'}})) {