array.length作为函数的键(固体)

发布于 2025-01-26 14:04:56 字数 866 浏览 1 评论 0原文

但是,我对新手问题表示歉意,但是我试图学习编码,没有其他方法来找到解决方案。我正在寻找一种将唯一键添加到功能结果中的唯一键的方法,以便在需要时可以使用相同功能的其他迭代来参考它。

请在下面找到代码。当我在其立场时运行代码时,它一直要求用户在寻找代码不询问用户而是使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾旎 2025-02-02 14:04:56

您可以调用getTID()函数,并将返回值存储在临时的UINT中,然后将临时UINT推到数组中。这样的事情:

// 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(
    string memory _title1,
    string memory _title2,
    string memory _title3,
    string memory _title4,
    string memory _title5
    
) public {
    uint256 _nextId = gettid();
    inputsArray.push(Thread(_nextId, _title1, _title2, _title3, _title4, _title5));
}}

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:

// 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(
    string memory _title1,
    string memory _title2,
    string memory _title3,
    string memory _title4,
    string memory _title5
    
) public {
    uint256 _nextId = gettid();
    inputsArray.push(Thread(_nextId, _title1, _title2, _title3, _title4, _title5));
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文