遍历一组哈希值 [英] Iterating through an array of hashes

查看:147
本文介绍了遍历一组哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的例程,遍历散列0 - 7并打印每个中特定键的值。我需要在每个哈希中获取'b4'的值。



我想取消(0..7),其中有些东西比较聪明是不同数量的哈希。例如,有时只有2个,或者可能有160个。

  my $ out = decode_json $ client-> responseContent( ); 

#print\\\
Output是:\\\
\\\
。 Dumper $ out;
为我的$ slice(0..7){
my $ out = $ out-> {data} [$ slice] {b4};
打印$ out \\\
;

$ / code>

数据结构如下:

  DB< 1> x $ out 
0 HASH(0x125fb5e0)
'data'=> ARRAY(0x1260d760)
0 HASH(0x121765d0)
'b1'=> '21'
'b2'=> '22'
'b3'=> '23'
'b4'=> '24'
'b5'=> '25'
'b6'=> '26'
'b7'=> '27'
1 HASH(0x125fb650)
'b1'=> '21'
'b2'=> '22'
'b3'=> '23'
'b4'=> '24'
'b5'=> '25'
'b6'=> '26'
'b7'=> '27'
2 HASH(0x1236b960)
'b1'=> '21'
'b2'=> '22'
'b3'=> '23'
'b4'=> '24'
'b5'=> '25'
'b6'=> '26'
'b7'=> '27'
3 HASH(0x12177030)
'b1'=> '21'
'b2'=> '22'
'b3'=> '23'
'b4'=> '24'
'b5'=> '25'
'b6'=> '26'
'b7'=> '27'
4 HASH(0x1260da00)
'b1'=> '21'
'b2'=> '22'
'b3'=> '23'
'b4'=> '24'
'b5'=> '25'
'b6'=> '26'
'b7'=> '27'


解决方案

您的 $ out 是对单元素散列的引用,它对其 data 元素的值有一个数组引用



最好将参考提取到一个单独的变量中,以便您可以更简单地访问内容。假设你写了

  my $ data = $ out-> {data}; 

此后,数组可以以 @ $ data scalar @ $ data ,并且索引是 0 .. $#$ data 。您可以使用 $ data-> [0] $ data-> [1] 等。



您的代码将变成

  my $ out = decode_json $ client-> responseContent; 
my $ data = $ out-> {data};

用于我的$ i(0 .. $#$ data){
my $ item = $ data-> [$ i];
my $ b4 = $ item-> {b4};
打印$ b4\\\
;
}

但请注意,除非您需要将数组索引用于其他目的,否则可能会更好地遍历数组元素本身而不是其索引。这段代码可以做同样的事情

  my $ out = decode_json $ client-> responseContent; 
my $ data = $ out-> {data};

用于我的$ item(@ $ data){
my $ b4 = $ item-> {b4};
打印$ b4\\\
;
}

甚至只需

 为@ $ data打印$ _-> {b4} \\\
;

如果您不需要在循环中执行其他任何操作


I have wrote the below routine, to iterate through hashes 0 - 7 and print out the value of a specific key in each. I need to grab the value of 'b4' in each hash.

I would like to do away with the (0..7), with something smarter for when there are different numbers of hashes. For instance, sometimes there is only 2 or there may be 160.

my $out = decode_json $client->responseContent();

#print "\nOutput is :\n\n" . Dumper $out;
for my $slice (0..7) {
    my $out = $out->{data}[$slice]{b4};
    print " $out \n";
}

The data is structured as such:

  DB<1> x $out
0  HASH(0x125fb5e0)
   'data' => ARRAY(0x1260d760)
      0  HASH(0x121765d0)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      1  HASH(0x125fb650)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      2  HASH(0x1236b960)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      3  HASH(0x12177030)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      4  HASH(0x1260da00)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'

解决方案

Your $out is a reference to a single-element hash which has an array reference for the value of its data element

It's best to extract the reference into a separate variable so that you can access the contents more simply. Suppose you wrote

my $data = $out->{data};

Thereafter, the array is accessible as @$data, the number of elements it contains is scalar @$data, and the indices are 0 .. $#$data. You can access each element of the array with $data->[0], $data->[1] etc.

Your code would become

my $out  = decode_json $client->responseContent;
my $data = $out->{data};

for my $i ( 0 .. $#$data ) {
    my $item = $data->[$i];
    my $b4 = $item->{b4};
    print "$b4\n";
}

But note that, unless you need the array index for other purposes, you are probably better off iterating over the array elements themselves rather than its indices. This code would do the same thing

my $out  = decode_json $client->responseContent;
my $data = $out->{data};

for my $item ( @$data ) {
    my $b4 = $item->{b4};
    print "$b4\n";
}

Or even just

print "$_->{b4}\n" for @$data;

if you don't need to do anything else within your loop

这篇关于遍历一组哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆