我试图在同一类型的多个结构上映射一个地址,它属于同一个地址。如果我想在之后的请求中为一个地址选择任何“存储”结构,我该怎么做呢?
我创建了一个名为Prescription的结构体,以及患者地址的映射。所以我真正想要的是将患者地址映射到几个处方结构。
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
}
mapping (address => Prescription) ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string medicament, string dosage_form, uint amount, uint date) public restricted {
var newPrescription = ownerOfPrescription[patients_address];
newPrescription.medicament = medicament;
newPrescription.dosage_form = dosage_form;
newPrescription.amount = amount;
newPrescription.date = date;
patients.push(patients_address) -1;
}
function getPre(address _address)view public returns (string, string, uint, uint){
return(
ownerOfPrescription[_address].medicament,
ownerOfPrescription[_address].dosage_form,
ownerOfPrescription[_address].amount,
ownerOfPrescription[_address].date);
}
现在我有一个功能,我可以为一个病人打电话给所有书面处方。实际上我只能拨打一个地址的最后一个书面处方。
当然,mapping
的值类型可以是一个数组:
// map to an array
mapping (address => Prescription[]) ownerOfPrescription;
function createPrescription(...) ... {
// add to the end of the array
ownerOfPrescription[patients_address].push(Prescription({
medicament: medicament,
...
});
patients.push(patients_address);
}