5

For example following code piece from https://ethereum.stackexchange.com/a/10763/4575 . I have added new lines that is followed by //*.

pragma solidity ^0.4.2;
contract test {    
  struct my_struct {
    int a;
    int my_length; //*to keep track of the length.
  }

  mapping (address=>my_struct[]) Map;

  function fill_map(my_struct struct1,my_struct struct2) internal  {    
    Map[msg.sender].push(struct1);
    Map[msg.sender][0].my_length = Map[msg.sender][0].my_length + 1;//*

    Map[msg.sender].push(struct2);   
    Map[msg.sender][0].my_length = Map[msg.sender][0].my_length + 1;//*

    Map[msg.sender].push(struct3);//*  
    Map[msg.sender][0].my_length = Map[msg.sender][0].my_length + 1;//*

    delete Map[id][2]; //*struct3 will be removed.
    Map[msg.sender][0].my_length = Map[msg.sender][0].my_length - 1;//*
  }
}

I can obtain data as follows: Map[id][index].a; index is an integer number. In this example it can be 0 (for struct1) or 1 (for struct2).

As we know Java could return the number of object inside a list:

String[] array = new String[10];
int size = array.length;

Is there anything similar as: Map[id].length ? or manually after each push or delete should I need to keep length? On my solution Map[msg.sender][0].my_length will return the number of the pointed structs from Map[msg.sender].

Thank you for your valuable time and help.

3

there is some mistakes (or i didn't understand well your question) in your previous code and i think also in your question.

in your case you have a map of arrays. so Map[key].length exists and return the size of the corresponding array to your key. so Map[msg.sender].length is correct but Map[msg.sender][0].length is not. because in this second case you are returning the first struct which doesn't have a length parameter but a my_length.

  • Ahh my bad, I am sorry for the confusion. I did not know Map[key].length actually exists, so I try to use variable my_length to keep track. It was a typo Map[msg.sender][0].my_length is the correct line. – alper Dec 23 '16 at 18:25
2

You can't get the number of mapped keys or get a list of keys that map to data.

You can maintain a list a keys in a separate array as you insert data into a map and then you can get the count indirectly by asking how long the index is.

If you use event emitters to report state changes, you can often proceed on the idea that the contract itself doesn't need the count or the list and the clients don't need further help from the contract.

  • Whoa, the question changed a lot. I'm confused about what we're trying to accomplish. We would be able to figure out how many structs are stored at a give address map (array of structs), but not how many addresses have structs. Hope that makes sense. – Rob Hitchens - B9lab Dec 23 '16 at 18:12

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.