显示对象数组中的数据

发布于 2024-10-19 02:13:05 字数 943 浏览 2 评论 0原文

我正在尝试显示使用另一家公司的 API 获得的对象数组中的数据,但是当我尝试使用 foreach 循环时出现错误。

我正在使用 Dumper 显示数组中的所有内容。

print Dumper($object);

Dumper 的部分输出:

'enable_dha_thresholds' => 'false',
  'members' => [
    bless( {
      'ipv4addr' => '192.168.1.67',
      'name' => 'name.something.com'
    }, 'Something::Network::Member' ),
    bless( {
      'ipv4addr' => '192.168.1.68',
      'name' => 'name.something.com'
    }, 'Something::Network::Member' )
  ],
  'comment' => 'This is a comment',

我正在尝试提取似乎是双数组的“成员”:

//this works    
print $members->enable_dha_thresholds(); 

//this works
print $members[0][0]->ipv4addr; 

//does not work
foreach my $member ($members[0]){
     print "IP". $member->ipv4addr()."\n";  
}

我收到此错误: 无法在 ./script.pl 第 12 行的 unblessed 引用上调用方法“ipv4addr”。

我不确定我是否完全理解 Perl 中的“blessed”与“unblessed”,因为我是该语言的新手。

I'm trying to display data from an array of objects obtained using another company's API, but I am getting errors when I attempt to using a foreach loop.

I'm using Dumper to display everything in the array.

print Dumper($object);

Partial output from Dumper:

'enable_dha_thresholds' => 'false',
  'members' => [
    bless( {
      'ipv4addr' => '192.168.1.67',
      'name' => 'name.something.com'
    }, 'Something::Network::Member' ),
    bless( {
      'ipv4addr' => '192.168.1.68',
      'name' => 'name.something.com'
    }, 'Something::Network::Member' )
  ],
  'comment' => 'This is a comment',

I'm trying to extract the "members" which appears to be a double array:

//this works    
print $members->enable_dha_thresholds(); 

//this works
print $members[0][0]->ipv4addr; 

//does not work
foreach my $member ($members[0]){
     print "IP". $member->ipv4addr()."\n";  
}

I receive this error:
Can't call method "ipv4addr" on unblessed reference at ./script.pl line 12.

I'm not sure I entirely understand "blessed" vs "unblessed" in Perl since I am new to the language.

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

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

发布评论

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

评论(2

久而酒知 2024-10-26 02:13:05

print $members[0][0]->ipv4addr; //这
作品

因此 $members[0] 是一个数组引用。
您必须取消引用该数组:

foreach my $member ( @{ $members[0] } ){
    print "IP". $member->ipv4addr()."\n";  
}

引用“未受祝福的引用”的错误告诉您没有使用对象;相反,您提供了一个数组引用,它不一样:)

HTH,
保罗

print $members[0][0]->ipv4addr; //this
works

so $members[0] is an array reference.
You have to dereference the array:

foreach my $member ( @{ $members[0] } ){
    print "IP". $member->ipv4addr()."\n";  
}

The error refering to an "unblessed reference" tells you you aren't using an object; rather you provide an array-reference, which isn't the same :)

HTH,
Paul

可爱暴击 2024-10-26 02:13:05

这是“数组引用”与“数组”的问题。 $members[0] 是一个数组引用; foreach 运算符适用于数组(或迂腐的列表)。您可能想说

foreach my $member ( @{$members[0]} ) { ...

要迭代 $members[0] 引用的元素。

语法很棘手,在使用这些东西的过程中您可能会犯更多错误。帮助您加快速度的相关文档位于 perlref (或perlreftut), perllol,以及 perldscperlobj


顺便说一句,“有福”意味着引用“知道”它是什么类型的对象以及它应该查找哪个包以查看它可以运行哪些方法。当您收到“未受祝福的引用”警告或错误时,通常意味着您在需要对象的地方传递了一些不是对象的东西 - 在本例中,$members[0] 是未受祝福的引用而您打算传递受祝福的引用 $members[0][0]$members[0][1] 等。

It's an issue of "array reference" vs. "array". $members[0] is an array reference; the foreach operator works with arrays (or lists, to be pedantic). You will want to say

foreach my $member ( @{$members[0]} ) { ...

to iterate over the elements that $members[0] refers to.

The syntax is tricky, and you will probably make a few more mistakes along the way with this stuff. The relevant docs to get you up to speed are in perlref (or perlreftut), perllol, and also perldsc and perlobj.


"blessed" by the way, means that a reference "knows" what kind of object it is and what package it should look in to see what methods it can run. When you get an "unblessed reference" warning or error, that usually means you passed something that was not an object somewhere that expected an object -- in this case, $members[0] is the unblessed reference while you intended to pass the blessed references $members[0][0], $members[0][1], etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文