我有一个映射表,其中将多个哈希存储到该表中。我想要做的是我希望用户使用 setinstructors() 函数添加另一个散列,然后尝试查看映射表中是否已经存在相同的散列。如果表中已经存在相同的哈希值,那么它应该返回 true esle false。这是我的代码:
pragma solidity ^0.4.18;
contract Hash{
bytes32 comphash;
struct hashstruct{
bytes32 fhash;
}
mapping (uint => hashstruct) hashstructs;
uint[] public hashAccts;
function setinstructor(uint _uint,string _fhash) public {
var a = hashstructs[_uint];
a.fhash = sha256(_fhash);
hashAccts.push(_uint) -1;
}
function getInstructor(uint ins) view public returns (bytes32) {
return (hashstructs[ins].fhash);
}
function count() view public returns (uint) {
return hashAccts.length;
}
function setinstructors(string _comphash) public {
comphash = sha256(_comphash);
}
function getInstructors() public constant returns (bytes32) {
return (comphash);
}
}
最佳答案
Solidity 中没有“存在”这样的东西 mapping
.
每个键都映射到某些东西。如果尚未设置任何值,则该值为 0。
对于您的用例,hashstructs[n].fhash > 0
大概就够了。如果要明确,请添加 bool exists
给您的 struct
并将其设置为 true
当你添加一些东西时。然后使用 hashstructs[n].exists
检查是否存在。
关于ethereum - 如何知道映射表中是否存在特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49637672/