0

If the Set function receives two inputs

1.["0xca35b7d915458ef540ade6068dfe2f44e8fa733c",50,"ROC","park"]

2.["0x14723a09acff6d2a60dcdf7aa4aff308fddc160c",60,"AU","school"]

How can I return all the time in the struct, because I want to use that time to do some if-else statement?

For example, I want to use the time to do some matching, if there are 40 users to input their time, location and position, the start function will use the time to do some matching(if-else), then send the matching result to the user.

pragma solidity ^0.5.1;

contract matching {

    struct User {
        uint time;
        string location;
        string position;
    }
    User[] people; 

    mapping (address => User) user_mapping;
    address[] public userAccounts;

    function Set(address _address, uint _time, string memory _location, string memory _position) public {
        User storage users = user_mapping[_address];

        users.time = _time;
        users.location = _location;
        users.position = _position;

        userAccounts.push(_address) -1;
    }

    function getAccounts()public view returns(address[] memory) {
        return userAccounts;
    }

    function getInfo(address _address) view public returns (uint, string memory, string memory) {
        return (user_mapping[_address].time, user_mapping[_address].location, user_mapping[_address].position);
    }

    function countInfo() view public returns (uint) {
        return userAccounts.length;
    }
    function start() public{
     //Use the time that store in struct to do some matching
    }

}
  • Do you want to access the time property for all instances of User struct? – saman.shahmohamadi Sep 8 at 19:00
0

You don't say why.

From the generality of the description, I get the impression that you are looking for a general method for ad-hoc queries as if it's convenient.

It's not. You have to anticipate every internal query the contract will need and construct the state organization accordingly. Have a look over here for some thoughts on this topic: https://medium.com/solidified/the-joy-of-minimalism-in-smart-contract-design-b67fb4073422

To identify users by time, possibly to decide if a transaction is acceptable, you could organize things with a mapping to help:

mapping (uint=>address []) ...

If the list is dynamic as I suspect might be the case, you will need a delete capability. Otherwise, pushing addresses to the array might be enough.

It's a general answer. I don't think it's possible to be more specific without more details.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.