Perl从哈希数组中填充哈希

时间:2014-08-01 01:27:43

标签: perl hash

我有一个脚本,我试图填充perl哈希 当我单独进行时,我可以将它们解除引用

while(my($key,$value) = each(%{$spec_hash{'XISX'}})) {
     print $key, "," .$value ;
     print "\n";
     }


while(my($key,$value) = each(%{$spec_hash{'XCBO'}})) {
     print $key, "," .$value ;
     print "\n";
     }

然而,当我只是尝试取消引用%spec_hash它只包含一个$ exch引用,而它应该有两个 - XISX和XCBO。 但它永远不会进入XCBO。

#!/sbcimp/dyn/data/EVT/GSD/scripts/perl/bin/perl
use FOOConf; # this is our custom DBI module
use Data::Dumper ;
FOOConf::makeDBConnection(production);
my $dbh=$FOOConf::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;
           }
$query = "select e_risk_symbol from gsd_etds where level_name='EXCH_CS' and e_exch_dest='XCBO' and e_combo_type='9999'";
if(!$dbh) {
     print "Error connecting to DataBase; $DBI::errstr\n";
        }
     $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{'XCBO'}{$row[0]}=1;
          }

#while(my($key,$value) = each(%spec_hash)) {
#     print $key, "," .$value ;
#     print "\n";
#     }
#
#     foreach my $exch (sort keys %spec_hash) {
#         print "$exch: $spec_hash{$exch}" ;
#         }

 print Dumper(\%spec_hash);

这是自卸车 - 翻车机是否也不应包含XCBO? 为什么哈希只有XISX元素?

$VAR1 = {
           'XISX' => {
                      '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,
                    }
        };

1 个答案:

答案 0 :(得分:1)

您确定要使用这些值填充它吗?

尝试在while循环中添加print语句,如下所示:

 while (my @row=$cur_msg->fetchrow_array) {
      $spec_hash{'XCBO'}{$row[0]}=1;
      print "DEBUG $row[0]\n";
 }

我的猜测是你的查询没有返回任何结果添加到哈希。除非我错过了什么,否则你的其他代码看起来很好。