ethereum - 如何知道映射表中是否存在特定值?

标签 ethereum solidity smartcontracts

我有一个映射表,其中将多个哈希存储到该表中。我想要做的是我希望用户使用 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/

相关文章:

node.js - 如何使用 eris 的 javascript 编译一段 Solidity 代码?

javascript - 为什么使用安全帽时华夫饼显示为未定义

network-programming - 以太坊的RLPx有什么用以及他们如何在以太坊生态系统中使用它

solidity - Remix 上没有自动完成/代码完成功能?

ethereum - web3.eth.abi.decodeLog 返回错误的日志参数值

solidity - 尚不支持将 struct memory[] 类型的内存复制到存储

ethereum - 如何判断接收方是否为流动性池?

ethereum - ERC721 实现

blockchain - 我们可以在智能合约中使用 Solidity 获取过去区 block 中记录的交易信息吗?

python-3.x - 使用 Python (py-solc) 运行 Solidity 时出错