array.length作为函数的键(固体)
但是,我对新手问题表示歉意,但是我试图学习编码,没有其他方法来找到解决方案。我正在寻找一种将唯一键添加到功能结果中的唯一键的方法,以便在需要时可以使用相同功能的其他迭代来参考它。
请在下面找到代码。当我在其立场时运行代码时,它一直要求用户在寻找代码不询问用户而是使用GetTID()函数产生的值的代码时要输入一个值。
由于我显然是对编码的新手,因此我也对改进关键标识符的建议开放(它必须是唯一的,尽管我的方法很简单,但它应该产生独特的价值)。
// spdx-license-ientifier:MIT
Pragma Solidity ^0.8.0;
合同示例{
struct Input{
uint256 gettid;
string title1;
string title2;
string title3;
string title4;
string title5;
}
Input[] public inputsArray;
function gettid() public view returns(uint256) {
return inputsArray.length;
}
function addinput(
uint256 _gettid,
string memory _title1,
string memory _title2,
string memory _title3,
string memory _title4,
string memory _title5
) public {
inputsArray.push(Thread(_gettid, _title1, _title2, _title3, _title4, _title5));
}}
My apologies for the newbie question however I am attempting to learn coding and have no other means of finding the solution. I am looking for a way to have a unique key added to the outcome of a function so I can have other iterations of that same function to refer to it if needed.
Please find the code below. When I run the code as it stands it keep asking for a value to be input by the user while I am looking for the code to not ask the user but to use the value that is produced from the gettid() function.
As I am clearly very new to coding so I am also open to suggestions as to improve on the key identifier (it needs to be unique and although my approach is simple it should produce unique values).
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract example{
struct Input{
uint256 gettid;
string title1;
string title2;
string title3;
string title4;
string title5;
}
Input[] public inputsArray;
function gettid() public view returns(uint256) {
return inputsArray.length;
}
function addinput(
uint256 _gettid,
string memory _title1,
string memory _title2,
string memory _title3,
string memory _title4,
string memory _title5
) public {
inputsArray.push(Thread(_gettid, _title1, _title2, _title3, _title4, _title5));
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以调用getTID()函数,并将返回值存储在临时的UINT中,然后将临时UINT推到数组中。这样的事情:
You can call the gettid() function and store the return value in a temporary uint and then push the temp uint to the array. Something like this: