I have a uint256 array with defined values. What I want to do is take the values from the array and assign them to a struct I'm initialising. Is this at all possible in Solidity?

Example:

struct Person {
uint256 age;
uint256 socialSecurity;
uint256 blaBlaBla;
...
}

uint256[] personInfo;

function assignInfo(uint256[] _personInfo) public {
for (uint256 i = 0; i < _personInfo.length; i++) {
Person memory _person = ({
age: [i]
socialSecurity: [i]
blaBlaBla: [i]
....
});
}
}


a quick ex.

Edit: just realised this works (so far), but there must be a better way?

Person memory _person = ({
age: _personInfo[0],
socialSecurity: _personInfo[1],
blaBlaBla: personInfo[2]
....
});

Jun 30, 2018, 10:02 PM
you cannot convert dinamic array to struct - they have different data layout. you can try to convert between fixed size array and struct though.
Jul 1, 2018, 5:39 AM

© 2024 Draquery.com All rights reserved.