I’m sure there is a "shortcut" to this problem – which I thought I’d solved, but which now doesn’t work. Suggestions please.
Having decoded the JSON object to "$codes", this works:
$codes->{"product"}->{"ad_id"}->{"$ad_id"}->{"singlePay"}=$singlePayments;
$codes->{"product"}->{"ad_id"}->{"$ad_id"}->{"trialPay"}=$trialPayments;
$codes->{"product"}->{"ad_id"}->{"$ad_id"}->{"subsPay"}=$subsPayments;
but it is somewhat repetative to add 10 entries in this way. I’m SURE I found a solution on SO – which I now can’t find. In my script, it is:
$codes->{"product"}->{"ad_id"}->{"$ad_id"}->{
"singlePay"=$singlePayments,
"trialPay"=$trialPayments,
"subsPay"=$subsPayments
};
But I get "Can’t modify constant item in scalar assignment …" I have also tried removing the quotes, and using "fat arrows" on the entries to add, but that doesn’t help. Probably a "schoolboy error", but where am I going wrong?
(As I operate between "PHP" and "Perl", perhaps this structure only works in PHP)
Also, I need to add another object to the entry, so tried "affs"={}
and "affs:"={}
within the brackets … but that created an error too. I could use the "long" line shown above, but be helpful to do it all in one operation. Here is the required existing structure:
"codes":{
"product":{
"ad_id":{
"itemNo1":{
singlePay":"0.00",
"trialPay":"0.00",
"subsPay":"0.00",
"affs":{}
}
}
}
}
2
Answers
See, I told ya it was "schoolboy". The line:
Should have been:
(The last short arrow (->) before the curly bracket should have been an equal sign!! Only taken me three hours to spot that typo !!)
To create a new hash/object, you could use the following:
To add to an existing hash/object (creating it if it doesn’t exist) without clobbering any other existing fields, you could use the following:
We could try other things, like a hash slice.
Not great.
How about merging hashes?
More expensive, and still not really any better than the solution with which we started.
Finally, how about a multi-variable foreach loop?
This is clean, but it’s hard to see an advantage (with a fixed number of assignments). And it requires 5.36 which may not be available.