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]
.