0

It´s posible to save structs inside array and all this array´s inside the same array. I mean:

[ [{"struct"}, {"struct"}, {"struct"}], [ {"struct"}, {"struct"}], [{"struct"}, {"struct"}, {"struct"}, {"struct"}] ]

I can´t find something related to this in the web.

And is efficient to implement this storage system. If the array is so big can increment the transaction cost. Is profitable?

1 Answer 1

1

Yes, you can have an array of array of struct.

pragma solidity >= 0.4.25 < 0.6;

contract Bank {
    struct Customer {
        uint age;
    }
    Customer[][] customers;
    function foo() public {
        if (customers.length > 0) {
            Customer[] storage northBranch = customers[0];
            if (northBranch.length > 1) {
                Customer storage customer = northBranch[1];
                customer.age = 23;
            }
        } else {
            Customer memory bob;
            bob.age = 34;
            customers.length += 1;
            customers[0].push(bob);
        }
    }
}

For the other answers I'd say it depends on the particular use.

3
  • Nice it look´s that work. But I can´t look for the parameters. How can I recover the information of one variable of the struct inside of one array that is inside the main array. thx Dec 27, 2018 at 8:45
  • And why you use storage?? Is not more efficient memory?? Dec 27, 2018 at 8:52
  • 1
    @Eduardo It depends on the use, if you assign to a memory variable you will be reading the whole struct from storage which is 5k gas for every slot. But subsequent reads will be cheaper because you will be reading from memory. Also memory variables in memory will be discarded when the transaction finished if you do not save it back into storage.
    – Ismael
    Dec 27, 2018 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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