Reputation: 365
I will write a smart contract about patient medical records. And I have an example for this. But all data stored in struct. I want to use time-series data. As far as know, I have to use array of struct, but I don't know how can i do that?
Can you help me please?
contract MedicalHistory {
enum Gender {
Male,
Female
}
uint _patientCount = 0;
struct Patient {
string name;
uint16 age;
//max of uint16 is 4096
//if we use uint8 the max is uint8
string telephone;
string homeAddress;
uint64 birthday; //unix time
string disease; //disease can be enum
uint256 createdAt; // save all history
Gender gender;
}
mapping(uint => Patient) _patients;
function Register(
string memory name,
uint16 age,
string memory telephone,
string memory homeAddress,
uint64 birthday,
string memory disease,
// uint256 createdAt,
Gender gender
}
}
This is the code snippet from my smart contract.. How can I convert struct to array?
Upvotes: 0
Views: 440
Reputation: 43561
You can .push()
into a storage array, effectively adding new item.
I simplified the code example just so it's easier to see the actual array manipulation:
pragma solidity ^0.8;
contract MedicalHistory {
struct Patient {
string name;
uint16 age;
}
Patient[] _patients;
function Register(
string memory name,
uint16 age
) external {
Patient memory patient = Patient(name, age);
_patients.push(patient);
}
}
Please note that if you're using a public network such as Ethereum, all stored data is retrievable even though it's stored in a non-public
property by querying the contract storage slots. See this answer for a code example. So unless this is just an academic exercise, I really don't recommend storing health and other sensitive data on the blockchain.
Upvotes: 1