簡體   English   中英

如何在 perl mojo 中獲取 hash 的 hash 鍵和值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM