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

时间:2018-01-15 17:55:06

标签: algorithm perl mariadb hashtable

我想将从数据库中获取的值存储到哈希中。然后,我想比较并查看它们是否具有相同的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,
    },
  );
}

1 个答案:

答案 0 :(得分:3)

The following replaces all existing values in the hash:

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

Replace the above code with the following:

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

This assumes the values of $material are unique.