[英]How to get the hash keys and values of a hash in perl mojo
我試圖弄清楚如何 select 顯式鍵和 hash 的值。
my $hash = query('select id, name, required from table')->hashes
我轉儲時的 output 是:
var1 = bless ([
{
'name' => value,
'id' => value,
'required' => value
}.....
])
我想要的是得到以下 output:
var1 = bless ([
{
'required' => value
}...
])
之后,我想比較另一個數組的索引 == 索引。
您從bless
中刪除了 class 名稱,但我猜它是Mojo::Collection 。 使用它的map
方法迭代元素:
my $required = $hash->map(sub { required => $_->{required} });
# Untested.
此外,將名稱$hash
用於哈希集合是令人困惑的。
我不明白你關於索引的最后一句話。 如果要提取$index
-th 元素,可以使用
my $hash = $required->to_array->[$index];
或直接獲取價值
my $value = $required->to_array->[$index]{required};
我用其他代碼解決了這個問題。 不再需要上面的 function。
一旦您獲得 hash 參考,無論通過何種方式,您都可以像這樣訪問密鑰:
$hash->{required}; # etc.
有關更多信息,請參見perlref 。 另請閱讀 perldsc 和 perlreftut。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.