【问题标题】:How to append values taken from SQL to multiple hashes and then compare them如何将取自 SQL 的值附加到多个哈希值,然后进行比较
【发布时间】:2018-06-24 09:32:38
【问题描述】:

我想将从我的数据库中获取的值存储到哈希中。然后,我想比较一下它们是否具有相同的material。如果他们有相同的material,我想比较txt。如果txt 不同,我想使用storedisp。如果文字相同,我想用coursemat:

my $stmt1 = qq(select txt, price, material from coursemat);
my $sth1 = $pagev->runQ($stmt1); #run query in house function
my $stmt2 = qq(select material from storedisp);
my $sth2 = $pagev->runQ($stmt2);

while(my ($txt, $price, $material) = $sth->fetchrow_array) {
  %cmhash = (
    $material => {
      txt => $txt,
      price => $price,
    },
  );
}

while(my $txt = $sth->fetchrow) {
  %sdhash = (
    $material => {
      txt => $txt,
    },
  );
}

【问题讨论】:

标签: algorithm perl mariadb hashtable


【解决方案1】:

以下内容替换散列中的所有现有值:

%cmhash = (
  $material => {
    txt => $txt,
    price => $price,
  },
);

将上面的代码替换为:

$cmhash{$material} = {
    txt => $txt,
    price => $price,
};

这假定$material 的值是唯一的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多