如何在Perl中将arrayref转换为hashref?

时间:2018-05-23 12:31:28

标签: perl

我有一个像这样的哈希数组:

[
    {
        'id'    => '6',
        'image' => '/x/eng/rlse/DOT/R9.0xN/final/bedrock/export/x86_64/tarball/image.tgz'
    },
    {
        'new_netboot_image' => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/final/bedrock/export/x86_64/netboot/netboot/kernel',
        'version'           => '9.2',
        'ntest'             => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/test/nate/bin/ntest',
        'nate_lib'          => '/x/eng/ctl/test-tools;/x/eng/ctl/test-tools/
    }
];

我想将此传递给仅接受哈希引用的子例程。我该怎么做呢?以上输出来自以下转储器。

$Log->info("Dumping array after updating" . Dumper($orig_cleanup_version_settings));
        $Api->service_db->cleanup_version_mapping_update($orig_cleanup_version_settings);

3 个答案:

答案 0 :(得分:1)

看起来您需要将数组中的哈希值聚合为单个哈希

猜测数据结构的构建不正确,导致两个哈希值而不是一个哈希值。最好的方法是首先正确构建哈希,但是这段代码会为你组合数据

my $data = [
    {
        'id'    => '6',
        'image' => '/x/eng/rlse/DOT/R9.0xN/final/bedrock/export/x86_64/tarball/image.tgz'
    },
    {
        'new_netboot_image' => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/final/bedrock/export/x86_64/netboot/netboot/kernel',
        'version'           => '9.2',
        'ntest'             => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/test/nate/bin/ntest',
        'nate_lib'          => '/x/eng/ctl/test-tools;/x/eng/ctl/test-tools/
    }
];

my %params;
{
    %params = ( %params, %$_ ) for values %$data;
}

my_sub(\%params);

答案 1 :(得分:1)

这是一种根据您的规范创建哈希引用的方法。我的transaction元素毫无根据地推测它与输入中的id相同。

#!/usr/bin/env perl

use Modern::Perl;
use Data::Dumper;

my $a = [
    {
        'id'    => '6',
        'image' => '/x/eng/rlse/DOT/R9.0xN/final/bedrock/export/x86_64/tarball/image.tgz'
    },
    {
        'new_netboot_image' => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/final/bedrock/export/x86_64/netboot/netboot/kernel',
        'version'           => '9.2',
        'ntest'             => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/test/nate/bin/ntest',
        'nate_lib'          => '/x/eng/ctl/test-tools;/x/eng/ctl/test-tools/'
    }
];

my $h = { map { %$_ } @$a };
$h->{ transaction } = delete $h->{ id };
print Dumper $h;

输出:

$VAR1 = {
          'image' => '/x/eng/rlse/DOT/R9.0xN/final/bedrock/export/x86_64/tarball/image.tgz',
          'new_netboot_image' => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/final/bedrock/export/x86_64/netboot/netboot/kernel',
          'version' => '9.2',
          'ntest' => '/x/eng/bbnbs/daemon/DOT/R9.2x/cit-ok/test/nate/bin/ntest',
          'nate_lib' => '/x/eng/ctl/test-tools;/x/eng/ctl/test-tools/',
          'transaction' => '6'
        };

答案 2 :(得分:0)

使用ref()测试传递的值。在下面的示例中,我们传递一个哈希值和两个哈希引用,请注意我们只打印两次值,即使传递了三个哈希值(两个为refs)。字符串:" HASH" " ARRAY" " SCALAR"可用于测试对结构的引用,以及类的包字符串,例如" Foo :: Bar"可用于测试对象。

#! /bin/perl

use strict ; 

my %foo = ('foo' => 'bar') ; # hash
my $reffoo = \%foo ; # hashref
my $refbar = {'bar' => 'foo'} ; # hashref

sub only_hashref {
    return undef unless (ref($_[0]) eq 'HASH') ; 
    print ($_[0]->{'foo'}, "\n") if defined $_[0]->{'foo'} ; 
    print ($_[0]->{'bar'}, "\n") if defined $_[0]->{'bar'} ; 
}

only_hashref(%foo) ; 
only_hashref($reffoo) ; 
only_hashref($refbar) ;