如何将从 SQL 中获取的值附加到多个哈希值,然后比较它们

How to append values taken from SQL to multiple hashes and then compare them

我想将从我的数据库中获取的值存储到散列中。然后,我想比较一下,看他们有没有相同的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,
    },
  );
}

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

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

用以下代码替换上面的代码:

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

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