Perl通过哈希循环

Perl通过哈希循环,perl,Perl,我试图解析一个API响应,并创建一个数组 my $data1 = $value->{_embedded}->{'rh:coll'}; print(Dumper($data1)); 输出: $VAR1 = [ { '_etag' => { '$oid' => '571e0eb10fdcb17d700e586b' }

我试图解析一个API响应,并创建一个数组

 my $data1 = $value->{_embedded}->{'rh:coll'};
 print(Dumper($data1));
输出:

$VAR1 = [
            {
              '_etag' => {
                           '$oid' => '571e0eb10fdcb17d700e586b'
                         },
              '_id' => 'example.com',
              'server_id' => '1',
              'enabled' => '1'
            },
            {
              '_etag' => {
                           '$oid' => '571e0eb90fdcb17d700e586c'
                         },
              '_id' => 'example10.com',
              'server_id' => '1',
              'enabled' => '1'
            }
          ];
我能够使用

print $value->{_embedded}->{'rh:coll'}->[0]->{_id} . "\t\n";
print $value->{_embedded}->{'rh:coll'}->[1]->{_id} . "\t\n";
输出

example.com
example10.com
如果启用设置为1,如何从网站名创建数组

我试过循环思考这个问题

foreach my $x (%$data1) {
        print $x->{_id};
 }

这里的
$data1
是数组引用,所以您应该使用
@{..}
取消对它的引用。也许你想要这样的东西:

my @sites;

foreach my $x( @ { $data1 } ) { 
  push( @sites, $x -> {_id} ) if( $x -> {enabled} ); # $x is hash reference
}

# now @sites contain all your sites from API response for which enabled is set to 1.

这里的
$data1
是数组引用,所以您应该使用
@{..}
取消对它的引用。也许你想要这样的东西:

my @sites;

foreach my $x( @ { $data1 } ) { 
  push( @sites, $x -> {_id} ) if( $x -> {enabled} ); # $x is hash reference
}

# now @sites contain all your sites from API response for which enabled is set to 1.

map{${${uID}}grep{${${u->{enabled}}}@$data1
map{${${uID}}grep{${${uID}}}}}@$data1