0

In this part of my code, The function newCarOwner creates a new Customer struct "carowner" and maps it to the input address under the map "carowners", and pushes each address to the array "carownerAdds". Now, I can look up each customer's information by entering their address (and get a list of addresses with the array).

However, I also want to be able to look up customers by their license number, which is also part of the information in the Customer struct. I know I cannot create an array for licenses, as these will be strings, but I feel I can still create a (string => Customer) mapping. I'm sure I could just make a new function to create a new Customer name such as "carownerlicenses", and have it triggered within the "newCarOwner" function, but this still seems like it would be a waste of space to have two separate mappings with the same information in each.

Now, I have seen contracts that have mappings within mappings, and thought that this might be how I could do it. I tried to map the "string" which will take _license, to the mapping "carowners", but it is not working. Is there any way to get this to work and if I do, will it allow me to retrieve the same customer info with the license? If not, is there another way I can do that? Thanks in advance.

mapping(address => Customer) carowners;
address[] public carownerAdds;

function newCarOwner(address _address, string _custname, string _color, string _make, uint32 _year, string _license) onlyMember public {

    Customer storage carowner = carowners[_address];
        carowner.custname = _custname;
        carowner.color = _color;
        carowner.make = _make;
        carowner.year = _year;
        carowner.license = _license;

        carownerAdds.push(_address);

        }

mapping(string => mapping(address => Customer)) carLicenses;

function mapLicense(string _license, address _address) public {
    carowners[_address] = carLicenses[_license];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.