Perl:访问和处理数组中的对象
Perl: Accessing and handling objects in an array
我尝试处理以下由 get_pool_member_object_status_($pool)
生成的数组
$VAR1 = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
我不知道这应该如何工作。我尝试了几个来自 perldsc 的例子。那里的例子主要涉及哈希数组。但是上面的数组是...?我什至不知道这是一个什么样的数组。有人能给我指出正确的方向吗?
例如,我如何访问 "member"->"address"
的 "object_status"->"availability_status"
谢谢辛巴克。这是我从 Data::Printer:
得到的输出
[
[0] [
[0] LocalLB::PoolMember::MemberObjectStatus[] {
public methods (0)
private methods (0)
internals: [
[0] LocalLB::PoolMember::MemberObjectStatus,
[1] LocalLB::PoolMember::MemberObjectStatus,
[2] LocalLB::PoolMember::MemberObjectStatus,
[3] LocalLB::PoolMember::MemberObjectStatus
]
}
]
]
所以您正在尝试访问 'availability_status'
和 'address'
我没有遍历整个对象,但是使用这段代码我能够打印所需的字段。
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $obj = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
print Dumper($obj->[0]->[0]->[0]->{member}->{address});
print Dumper($obj->[0]->[0]->[0]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[1]->{member}->{address});
print Dumper($obj->[0]->[0]->[1]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[2]->{member}->{address});
print Dumper($obj->[0]->[0]->[2]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[3]->{member}->{address});
print Dumper($obj->[0]->[0]->[3]->{object_status}->{availability_status});
输出:
$VAR1 = '192.168.100.141';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.142';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.143';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.144';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
希望这对您有所帮助,现在您应该能够使用循环遍历此数据结构了。
我尝试处理以下由 get_pool_member_object_status_($pool)
$VAR1 = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
我不知道这应该如何工作。我尝试了几个来自 perldsc 的例子。那里的例子主要涉及哈希数组。但是上面的数组是...?我什至不知道这是一个什么样的数组。有人能给我指出正确的方向吗?
例如,我如何访问 "member"->"address"
"object_status"->"availability_status"
谢谢辛巴克。这是我从 Data::Printer:
[
[0] [
[0] LocalLB::PoolMember::MemberObjectStatus[] {
public methods (0)
private methods (0)
internals: [
[0] LocalLB::PoolMember::MemberObjectStatus,
[1] LocalLB::PoolMember::MemberObjectStatus,
[2] LocalLB::PoolMember::MemberObjectStatus,
[3] LocalLB::PoolMember::MemberObjectStatus
]
}
]
]
所以您正在尝试访问 'availability_status'
和 'address'
我没有遍历整个对象,但是使用这段代码我能够打印所需的字段。
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $obj = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
print Dumper($obj->[0]->[0]->[0]->{member}->{address});
print Dumper($obj->[0]->[0]->[0]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[1]->{member}->{address});
print Dumper($obj->[0]->[0]->[1]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[2]->{member}->{address});
print Dumper($obj->[0]->[0]->[2]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[3]->{member}->{address});
print Dumper($obj->[0]->[0]->[3]->{object_status}->{availability_status});
输出:
$VAR1 = '192.168.100.141';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.142';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.143';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.144';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
希望这对您有所帮助,现在您应该能够使用循环遍历此数据结构了。