我有一些JSON存储在数据库列中,如下所示:
pokeapi=# SELECT height FROM pokeapi_pokedex WHERE species = 'Ninetales';
-[ RECORD 1 ]------------------------------------------
height | {"default": {"feet": "6'07\"", "meters": 2.0}}
作为我正在研究的'生成'算法的一部分,我想将此值转换为%hash,将其乘以(0.9..1.1).rand
(以允许'高度自然10%的变化),然后创建一个新的%hash在同一结构中。我的select-height
方法看起来像这样:
method select-height(:$species, :$form = 'default') {
my %heights = $.data-source.get-height(:$species, :$form);
my %height = %heights * (0.9..1.1).rand;
say %height;
}
这实际上是我的get-height
例程,以获得该物种的“平均”高度(公制和英制)。
method get-height (:$species, :$form) {
my $query = dbh.prepare(qq:to/STATEMENT/);
SELECT height FROM pokeapi_pokedex WHERE species = ?;
STATEMENT
$query.execute($species);
my %height = from-json($query.row);
my %heights = self.values-or-defaults(%height, $form);
return %heights;
}
但是我在执行时遇到以下错误(我假设因为我试图将哈希作为一个整体而不是散列的各个元素):
$ perl6 -I lib/ examples/height-weight.p6
{feet => 6'07", meters => 2}
Odd number of elements found where hash initializer expected:
Only saw: 1.8693857987465123e0
in method select-height at /home/kane/Projects/kawaii/p6-pokeapi/lib/Pokeapi/Pokemon/Generator.pm6 (Pokeapi::Pokemon::Generator) line 22
in block <unit> at examples/height-weight.p6 line 7
有没有更容易(和工作)的方式来做这个而不重复每个元素的代码? :)
首先,代码的逻辑存在问题。最初,你得到一个值的散列,"feet": "6'07\"", "meters": 2.0
解析出json,meters
是一个数字,feet
是一个字符串。接下来,您尝试将其乘以随机值...虽然它适用于数字,但它不适用于字符串。 Perl 6 allomorphs允许你这样做,实际上:say "5" * 3
将返回15
,但X"Y'
模式足够复杂,Perl 6不能自然地理解它。
因此,您可能需要在处理之前进行转换,然后再将其转换回来。
第二件事是导致您正在观察的错误的确切线。
考虑一下:
my %a = a => 5;
%a = %a * 10 => 5; # %a becomes a hash with a single value of 10 => 5
# It happens because when a Hash is used in math ops, its size is used as a value
# Thus, if you have a single value, it'll become 1 * 10, thus 10
# And for %a = a => 1, b => 2; %a * 5 will be evaluated to 10
%a = %a * 10; # error, the key is passed, but not a value
要直接处理哈希值,您需要使用map
方法并处理每对,例如:%a .= map({ .key => .value * (0.9..1.1).rand })
。
当然,它可以用其他方式打高尔夫球或书写,但主要问题是通过这种方式解决的。
你接受了@ Takao的回答。该解决方案需要手动挖掘到%hash
以获取叶片哈希/列表然后应用map
。
鉴于您的问题的标题提到“返回...相同的结构”,并且正文包含看起来像嵌套结构的内容,我认为重要的是提供了一些答案,提供了一些自动降序和复制嵌套结构的惯用解决方案:
my %hash = :a{:b{:c,:d}}
say my %new-hash = %hash».&{ (0.9 .. 1.1) .rand }
# {a => {b => {c => 1.0476391741359872, d => 0.963626602773474}}}
# Update leaf values of original `%hash` in-place:
%hash».&{ $_ = (0.9 .. 1.1) .rand }
# Same effect:
%hash »*=» (0.9..1.1).rand;
# Same effect:
%hash.deepmap: { $_ = (0.9..1.1).rand }
Hyperops(例如»
)迭代一个或两个数据结构来到他们的叶子,然后应用op为hypered:
say %hash».++ # in-place increment leaf values of `%hash` even if nested
.&{ ... }
使用方法调用语法调用大括号中的闭包。将此与hyperop结合使用可以写:
%hash».&{ $_ = (0.9 .. 1.1) .rand }
另一种选择是.deepmap
:
%hash.deepmap: { $_ = (0.9..1.1).rand }
hyperops和deepmap之间的关键区别在于允许编译器迭代数据结构并以任何顺序并行运行超级运算,而deepmap
迭代总是按顺序发生。