I don't think you need to use mappings or structs for your project. You should probably go to readthedocs.io to go over the arrays in Solidity. Arrays in Solidity are reference types, that's important to know about Solidity.
They have fixed arrays and dynamic arrays. This including mappings and structs are all reference types with their own issues.
A fixed array can never change in length which is very different to arrays in JavaScript.
For a fixed array in Solidity, no change in the length
and every single element must be of the same type.
Dynamic arrays in Solidity behave more like JavaScript arrays so it can change in length
over time.
Chances are you probably want to go with a dynamic array because I am assuming you want to allow as many people as possible to enter your lottery.
So, because you have not shared any code to what you have done so far, I will give you my own example of what I am talking about:
contract Test {
uint public myArray;
function Test() public {
myArray.push(1);
myArray.push(10);
myArray.push(30);
}
function getArrayLength() public view returns (uint) {
return myArray.length;
}
So here we have a contract called Test
which has an unsigned integer called myArray
.
Inside the constructor function for this contract you will see that myArray
has a method called push()
. It behaves the same as it does in JavaScript.
I can retrieve the length of the array with .length
.
contract Test {
uint public myArray;
function Test() public {
myArray.push(1);
myArray.push(10);
myArray.push(30);
}
function getArrayLength() public view returns (uint) {
return myArray.length;
}
function getFirstElement() public view returns (uint) {
return myArray[0];
}
Then we can access individual elements in the array using bracket notation and in Solidity arrays are also zero-indexed.
When I mark a variable as public
we automatically get a new function created for us that allows me to access the myArray
variable and it automatically will provide for me a function called msg
or message.
If you try to access myArray
, the function that gets generated does not return the entire array, it always accepts one argument and that is the index of the element you want to retrieve in the array.
With an array you have to ask for individual elements in Solidity, it will not give you all the elements in the array.
You can write a function to return the entire array like so:
function getMyArray() public view returns (uint[]) {
return myArray;
}
With this function you can get all the elements in your array, but you have to write a custom function like this, Solidity arrays does not do this by default.
Now this does not work for arrays of arrays or arrays of structs in Solidity.