I want to create a squad(group) which consists of a group addresses. Like Squad XYZ consists of Addr1 with name 'A',Addr2 with Name 'B', Addr3 with name 'C'.

pragma solidity ^0.5.9;

contract Squads{

    struct squadMemberInfo{
        string memberName;
        address memberAddress;
    struct squadInfo{
        string squadName;
         squadMemberInfo[] squadMembers;
    squadInfo[] public SI;
    squadMemberInfo[] public SMI;
    //Function to create a Squad/Group and name that group
    function createSquad(address[] memory _addressesToBeGrouped,string memory _squadName) public returns(string memory isCreated){
        //Yet to be coded
    //Function to add Members to an existing Squad
    function addMember(string memory _squadName,address _memberAddress, string memory _memberName) public returns(bool isAdded){
        squadMemberInfo memory squadMember = squadMemberInfo(_memberName,_memberAddress);
        squadInfo memory squadInformation = squadInfo(_squadName,SMI);
        return true;


I want make squadMemberInfo[] public SMI; as a local variable inside function addMember so that the function could return array of squadMemberInfo[]. I can directly use this return array and feed the struct squadInfo.

Also I am a bit confused how could I achieve this. :(


You should look into mappings. Think they can give you a solution. I see your struct squadInfo already has the squadMembers array so that should achieve the functionality you're looking for of grouping addresses. If you want to return those addresses, you can set up a getter function which looks up a squad and returns its members, or alternatively, you could give unique IDs to each squad and store squad members in a mapping(uint => address[]) squadMembers. As for giving each address a name, you can either use a mapping inside of your struct, to assign names within that context only, or use a global mapping so that names are transferrable accross squads.

This mapping would look like this: mapping(address => string) addressName.

To set values for mappings just use addressName[ADDRESS HERE] = "name";.

Essentially, a lot of what you're doing can be done just with arrays but might add too much complexity. You should really read up on mappings because I think they'll really help you!

| improve this answer | |
  • Thanks a lot @Yakko majuri. That really helped me in getting things done. I am using mapping now which may the way a bit simpler. – Abdul Mukheem Shaik Aug 15 '19 at 2:50
  • Happy to have helped @AbdulMuqeem! – Yakko Majuri Aug 17 '19 at 15:14

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.