perl 如何创建具有单键和多个值的哈希?

yduiuuwa  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(200)

我有一个正在访问的数据库,我的目标是将每一行都存储在哈希表中,其中id是关键字,其他行中存在的内容(即其他列)是值。实际上,我正在解析XLSX表并存储其值,之后必须应用逻辑来获取数据库值,然后比较XLSX表和DB的值。如果他们是相等的,那么测试是通过的,否则失败。我几乎花了3天,但没有得到任何结果。

输出

[0] = 1 1 https://au-sbc.trustidinc.com/tid sbcLab存根sbcLab SKY 0 2019-11-07 20:10:43 2021-07-02 04:39:43信任ID实验室Oracle Y Y Y ivr.本地域Y信任ID
2019-11-07 20:10:43 2020-12-14 06:24:17信任ID实验室Oracle Y Y Y ivr.本地域Y管理员

所需输出

哈希--〉关键字= 1
如果您是一名用户,请登录以下网址:http://au-sbc.trustidinc.com/tid。
类似地,对于循环中的其他迭代或id,其中id 2将是key 2,其值是key 2的值。x1c 0d1x

数据表看起来像

我使用的代码如下:

#!/usr/bin/perl
    use DBI;
    use strict;
    
    use warnings;
    use Switch;
    my @tstInfo;
    my %DbHash;
    
    my @carrierValuesAll=();
    my @carrierValuesAllGet="";
    
    if($dbType eq "mysql"){
    #$database = "trustid";
    $dsn = "DBI:$dbType:database=$database;host=$host:port=3306,DBSOCK = '/var/run/mysqld/mysqld.sock'";
    $userid = "XXXX";
    $password = 'XXXX';
    }else{
    $dbType="postgres";
    $userid = "postgres";
    $password = "postgres";
    $dsn = "DBI:$dbType:database=$database;host=$host:port=5432";
    }
    
    my $dbcon = DBI->connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
    print "connected to the database\n";
    
    my $notables = $dbcon->tables();
    print "No of tables : $notables"."\n" ;
    my @tables = $dbcon->tables(undef, undef, undef, 'TABLE');
    
    my $len = @tables;
    for (my $i = 0; $i < $len; $i = $i + 1)
        {
    print ("\@tables[$i] = $tables[$i]\n");
    print("\n");        
    } 
    my $sth = $dbcon->prepare('select * from active_table_set');
    
    $sth->execute();
     
    while (my @row = $sth->fetchrow_array()) {
       $database=join(',', @row);
       print($database."\n");
       if($dbType eq "mysql"){
    #$database = "trustid";
    $dsn = "DBI:$dbType:database=$database;host=$host:port=3306,DBSOCK = '/var/run/mysqld/mysqld.sock'";
    $userid = "XXXX";
    $password = 'XXXX';
    }else{
    $dbType="postgres";
    $userid = "postgres";
    $password = "postgres";
    $dsn = "DBI:$dbType:database=$database;host=$host:port=5432";
    }
    $dbcon = DBI->connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
    print "connected to the database\n";
    @tables = $dbcon->tables(undef, undef, undef, 'TABLE');
    $len = @tables;
    for (my $i = 0; $i < $len; $i = $i + 1)
        {
    print ("\@tables[$i] = $tables[$i]\n");
    print("\n");
    if($tables[$i] eq '`trustid_b`.`carriers`'){
        $sth = $dbcon->prepare('select * from carriers');
    
    $sth->execute();
    while (my @row = $sth->fetchrow_array()) {
        push(@carrierValuesAll, [ @row ]);
    }
    }
    
    } 
    }
    $len = @carrierValuesAll;
    for (my $i = 0; $i < $len; $i = $i + 1) {
        print ("\@carrierValuesAll[$i] = @{$carrierValuesAll[$i]}\n");
        print("\n"); 
        
        push(@tstInfo,\@carrierValuesAll[$i]);
    }
    for(my $i = 0; $i <= $#tstInfo; $i++){
    # push the test data  on the array
         $DbHash{$i+1} =  $tstInfo[$i];
    }
    print(%DbHash).("\n");

是否可以在此处创建哈希

while (my @row = $sth->fetchrow_array()) {
        push(@carrierValuesAll, [ @row ]);
    }

是的,那怎么办?
"我在做什么"
将每一行存储在标量中,输出如下:


其中,我尝试将我的每一行都推入@carrierValuesAll,然后将@carrierValuesAll推入@tstInfo,并尝试在此之后填充hash。我不知道如何访问它,是否正确。请帮助我。

qacovj5a

qacovj5a1#

我有一个正在访问的数据库,我的目标是将每一行存储在哈希中,其中id是关键字,其他内容(即其他列)作为值存在于行中
这可能不是你想要的,你真正想要的是一个两级哈希,其中第一级将id作为键,将哈希引用作为值,第二级将列名作为键Map到相关的值。
一种简单的方法是使用fetchrow_hashref()而不是fetchrow_array()

my %carriers;

while (my $row = $sth->fetchrow_hashref) {
  $carriers{$row->{id}} = $row;
}

相关问题