I have the following struct:

struct Group {
    string name;
    address[] members;
}

and the following array:

Group[] public groups;

I don't need values, I only want to add one address after the next to check easier if a user is in a group or not (without creating another array and pointing that array to the struct).

Now I know, that for example groups[0].members[0] could be msg.sender.

Is there a way to create like only one way arrays, for example, that it is members[msg.sender] instead of members[0,1,273,37382...]?

I edited the whole answer after your explanation.

I think what you are looking for is a mapping. I understand that your concern is that the previous answer does not organize by groups, but you can have that order as follows:

mapping(string => mapping(address => bool)) public Groups;

If you want to know if a particular address lets say 0x12345 is in the group called "GroupA" just need to do as follows:

if (mapping['GroupA'][0x12345]){
  //it exists!!
}else{
 //do not exists 
}

The whole thing can be implemented as follows as follows:

contract CheckAddress{
 mapping(string => mapping(address => bool)) public Groups;

 function CheckAddress(){
  }

 function setAddresToGroup(address addr, string group) public {
   Groups[group][addr] = true; // set an addres in a group
 }

 function checkExistenceInGroup(address addr,string group) public returns (bool){
   return Groups[group][addr]; // will return true if the address is in group
 }
}
  • Thank you and I know how to do it like you said, but tha's not how I would like to have it. That would create a huge array mapping. Instead I want for each group an own array like written in the main post. – dkb Mar 29 at 21:38
  • can you explain what do you mean by this: "Is there a way to create like only one way arrays, for example, that it is members[msg.sender] instead of members[0,1,273,37382...]?" – Jaime Mar 29 at 22:00
  • 1
    If I make an array like "address[] arrayName" I get values with calling arrayName[0], arrayName[1], arrayName[2],... etc. Those numbers than have the values, for example arrayName[0] could be "0x001...", arrayName[1] could be "0x123..." etc. But I want an array or other that has the address in the name itself. For example arrayName[0x001] or arrayName[0x123] etc. Because this would be easier to check. When I would check the first method I need to check each value if the address is the address I look for. In the second method I need only one check, if arrayName[msg.sender] exists. – dkb Mar 30 at 16:27
  • I modified the answer for a complete description of how to do this. I hope this will help you. Good luck! – Jaime Mar 30 at 18:18
  • Thank you! I have added an answer like exactly how I wanted it. :) – dkb Apr 2 at 15:55
up vote 0 down vote accepted

@Jaime thanks for your answer!

With your code I have found the solution I was looking for.
I wanted the members array right into the struct itself.
I edited the struct to the following:

struct Group {
    string name;
    mapping(address => bool) members; // <- ADDED THIS
}

Now I can do

groups[0].members[msg.sender] = true;

and get

if (groups[0].members[msg.sender] == true)



Thank you!

  • 1
    I'm glad I could help, and that you solve it. Good luck – Jaime Apr 5 at 14:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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