Perl:数组取消引用

发布于 2024-12-15 08:54:14 字数 2116 浏览 1 评论 0原文

我试图弄清楚如何取消引用数组中的值,但已经陷入了死胡同,我试图研究这个问题,但需要一些帮助。

我正在从 infoblox 数据库中获取一些数据,并尝试在结果中搜索以查找主机条目的 mac 地址,数据以由以下代码生成的哈希形式存在于数组中

use strict;
use Data::Dumper;
my @results = $session->get(
   object  => "Infoblox::DNS::Host",
   name    => "test.com.au",
   ipv4addrs   => ".*.",
   view    => "external"
);

:使用“$_->ipv4addrs”的高级数据结构,然后使用以下代码循环输出:

foreach (@results) {
    my @search = $_->ipv4addrs;        
         foreach (@search) {
             print Dumper($_) . "\n";
    }
}

使用 Data::Dumper 打印以下输出:

$VAR1 = [
      bless( {
               'network' => '111.111.111.0/25',
               'options' => [],
               'dynamic' => 'false',
               '__version' => '4.2r5-5-68691',
               'VIEW_FUNCTION' => {
                                    'remove' => '.com.infoblox.perl_api.Infoblox.DHCP.remove_fixed_address',
                                    'search' => '.com.infoblox.perl_api.Infoblox.DHCP.search_fixed_address',
                                    'add' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address',
                                    'add_using_template' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address_using_template',
                                    'get' => '.com.infoblox.perl_api.Infoblox.DHCP.get_fixed_address',
                                    'modify' => '.com.infoblox.perl_api.Infoblox.DHCP.modify_fixed_address'
                                  },
               'ipv4addr' => '111.111.111.111',
               'match_client' => 'MAC',
               'mac' => '00:11:00:11:00:11',
               'disable' => 'false',
               '__type' => '.com.infoblox.perl_api.Infoblox.DHCP.FixedAddr'
             }, 'Infoblox::DHCP::FixedAddr' )
    ];

但是如果我尝试使用以下命令调用引用当我在“foreach (@searh)”循环中使用“$_->mac”时,出现错误:

 "Can't call method "mac" on unblessed reference at ./connect_test.pl line nn."

我的编码技能此时失败,任何信息或方向将不胜感激。

I'm trying to figure out how to de-reference a value in an array but have hit a dead end, I've attempted to research the issue but need some help.

I'm grabbing some data from an infoblox database and trying to search within the results to find the mac address of a host entry, the data exists in the array as a hash generate by the following code:

use strict;
use Data::Dumper;
my @results = $session->get(
   object  => "Infoblox::DNS::Host",
   name    => "test.com.au",
   ipv4addrs   => ".*.",
   view    => "external"
);

I'm de-referencing at a high level of the data structure using '$_->ipv4addrs' then looping over the output using the following code:

foreach (@results) {
    my @search = $_->ipv4addrs;        
         foreach (@search) {
             print Dumper($_) . "\n";
    }
}

which prints the following output using Data::Dumper:

$VAR1 = [
      bless( {
               'network' => '111.111.111.0/25',
               'options' => [],
               'dynamic' => 'false',
               '__version' => '4.2r5-5-68691',
               'VIEW_FUNCTION' => {
                                    'remove' => '.com.infoblox.perl_api.Infoblox.DHCP.remove_fixed_address',
                                    'search' => '.com.infoblox.perl_api.Infoblox.DHCP.search_fixed_address',
                                    'add' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address',
                                    'add_using_template' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address_using_template',
                                    'get' => '.com.infoblox.perl_api.Infoblox.DHCP.get_fixed_address',
                                    'modify' => '.com.infoblox.perl_api.Infoblox.DHCP.modify_fixed_address'
                                  },
               'ipv4addr' => '111.111.111.111',
               'match_client' => 'MAC',
               'mac' => '00:11:00:11:00:11',
               'disable' => 'false',
               '__type' => '.com.infoblox.perl_api.Infoblox.DHCP.FixedAddr'
             }, 'Infoblox::DHCP::FixedAddr' )
    ];

But if I try and call a reference using '$_->mac' within the 'foreach (@searh)' loop, I get an error:

 "Can't call method "mac" on unblessed reference at ./connect_test.pl line nn."

My coding skills fail at this point, any info or direction would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-12-22 08:54:14

数组中的第一个元素是受祝福的引用。

$_->[0]->mac

您可以使用 -> 运算符直接访问数组引用中的元素(如上所述),或者将它们完全取消引用到列表:@list = @{$array_reference}

The first element in the array is the blessed reference.

$_->[0]->mac

You can directly access elements in array references with the -> operator (as above) or completely dereference them to a list: @list = @{$array_reference}.

硪扪都還晓 2024-12-22 08:54:14

@search 中存储的每个项目本身就是一个 arrayref。

# If arrayref contains only one     | # If multiple objects expected
# Infoblox::DHCP::FixedAddr object  | # inside @search
                                    |
                                    |
foreach ( @search ) {               | foreach my $item ( @search ) {
                                    | 
    my $obj = shift @$_;            |     foreach my $obj ( @$item ) {
    my $mac = $obj->mac;            |
}                                   |         my $mac = $obj->mac;
                                    |     }
                                    | }

Each item stored in @search is an arrayref itself.

# If arrayref contains only one     | # If multiple objects expected
# Infoblox::DHCP::FixedAddr object  | # inside @search
                                    |
                                    |
foreach ( @search ) {               | foreach my $item ( @search ) {
                                    | 
    my $obj = shift @$_;            |     foreach my $obj ( @$item ) {
    my $mac = $obj->mac;            |
}                                   |         my $mac = $obj->mac;
                                    |     }
                                    | }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文