英文:
Add multiple values to Perl / JSON object
问题
以下是您要翻译的内容:
"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 repetitive 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":{}
}
}
}
}
英文:
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":{}
}
}
}
}
答案1
得分: 3
以下是翻译好的代码部分:
创建新的哈希/对象,您可以使用以下方法:
$codes->{'product'}{'ad_id'}{$ad_id} = {
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => {},
};
要添加到现有的哈希/对象(如果不存在则创建),而不覆盖任何其他现有字段,您可以使用以下方法:
my $dst = $codes->{'product'}{'ad_id'}{$ad_id} //= {};
$dst->{'singlePay'} = $singlePayments;
$dst->{'trialPay'} = $trialPayments;
$dst->{'subsPay'} = $subsPayments;
$dst->{'affs'} = {};
我们可以尝试其他方法,比如哈希切片。
$codes->{'product'}{'ad_id'}{$ad_id}->@{qw(
singlePay
trialPay
subsPay
affs
)} = (
$singlePayments,
$trialPayments,
$subsPayments,
{},
);
不太好。
如何合并哈希?
my $dst = $codes->{'product'}{'ad_id'}{$ad_id} //= {};
%$dst = (%$dst,
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => {},
);
更昂贵,仍然不比我们最初的解决方案好。
最后,多变量的 foreach 循环如何?
my $dst = $codes->{'product'}{'ad_id'}{$ad_id} //= {};
for my ($k, $v) ( # 5.36+
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => {},
) {
$dst->{$k} = $v;
}
这是干净的,但很难看出有什么优势(对于固定数量的赋值)。而且它需要5.36,可能不可用。
英文:
To create a new hash/object, you could use the following:
$codes->{ product }{ ad_id }{ $ad_id } = {
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => { },
};
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:
my $dst = $codes->{ product }{ ad_id }{ $ad_id } //= { };
$dst->{ singlePay } = $singlePayments;
$dst->{ trialPay } = $trialPayments;
$dst->{ subsPay } = $subsPayments;
$dst->{ affs } = { };
We could try other things, like a hash slice.
$codes->{ product }{ ad_id }{ $ad_id }->@{qw(
singlePay
trialPay
subsPay
affs
)} = (
$singlePayments,
$trialPayments,
$subsPayments,
{ },
);
Not great.
How about merging hashes?
my $dst = $codes->{ product }{ ad_id }{ $ad_id } //= { };
%$dst = ( %$dst,
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => { },
);
More expensive, and still not really any better than the solution with which we started.
Finally, how about a multi-variable foreach loop?
my $dst = $codes->{ product }{ ad_id }{ $ad_id } //= { };
for my ( $k, $v ) ( # 5.36+
singlePay => $singlePayments,
trialPay => $trialPayments,
subsPay => $subsPayments,
affs => { },
) {
$dst->{ $k } = $v;
}
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.
答案2
得分: 0
"See, I told ya it was 'schoolboy'. The line:
$codes->{'product'}->{'ad_id'}->{'$ad_id'}->{
Should have been:
$codes->{'product'}->{'ad_id'}->{'$ad_id'}={
(The last short arrow (->) before the curly bracket should have been an equal sign!! Only taken me three hours to spot that typo !!)"
英文:
See, I told ya it was "schoolboy". The line:
$codes->{"product"}->{"ad_id"}->{"$ad_id"}->{
Should have been:
$codes->{"product"}->{"ad_id"}->{"$ad_id"}={
(The last short arrow (->) before the curly bracket should have been an equal sign!! Only taken me three hours to spot that typo !!)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论