I need a mapping of objects like this:

mapping (string => person) as clients;

struct person{ string name; uint score; }

let's say i have 3 persons on my table:
john doe
marcus aurelius
john mcfee

i need to somehow make a search from outside the smart contract in order to find all persons with john in the name, without executing any smart-contract function.

i understand i can't have a public mapping with string => person and converting string to bytes32 isn't viable because i need a bigger string to use as key.

how can i make both the keys and the objects public so i can search from outside the contract? should i use an auxiliary mapping or is there a more efficient way of doing this?


If you use the following design pattern, you can easily iterate over your mappings. That being said, just storing raw strings can be pretty gas costly, best to store the hashes of them.

uint256 public personCount;
mapping (uint256 => mapping (string => person)) private persons;
mapping (uint256 => string) private names;
function updatePersons(string _fN, uint256 _score) {
    persons[personCount]["hextet"] = person(_fN, _score);
    names[personCount] = "hextet";
    personCount = personCount + 1;

function iteratePersons() {
    for (uint256 i = 0; i < persons; i++) {
       string name = names[i];
       string fN   = persons[i][name].name;
       uint256 score = persons[i][name].score;
| improve this answer | |

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