请让我知道我在哪里以及我做错了什么......我只想在从数组中删除元素后逐个打印列表。
my $n = {};
my @current_list;
my @current_listremove;
@current_list = eval { $Table->invoke("get"); };
for ($n=0; $n < $size; $n++) {
my $currentIP=$current_list[$n][0];
if ( $MngtIP eq $currentIP ) {
# --- > I am able to get correct output below
DEBUG("DEBUG : Management IP $MngtIP exists in current list");
# --- > Here I am deleting the ip which matches
@current_listremove = delete $current_list[$currentIP];
}
}
# --- > Here I am trying to print the remaining list in array
my $updatedIP = $current_listremove[0][0];
my $size = @current_listremove;
for ($n=0; $n < $size; $n++) {
my $currentIP1=$current_listremove[$n][0];
DEBUG("DEBUG : Management IP updated list $currentIP1"); }
预期产出:
IP address in new list
100.100.100.1 -- if this Matched then remaining 4 should show in updated list
100.100.100.2
100.100.100.3
100.100.100.4
100.100.100.5
由于
为了更好地理解,下面是数组@current_list = $Table->invoke('get')
输出如下:
Intable data is stored in below format
{
{
100.100.100.1
Device1|Location1
}
{
100.100.100.2
Device2|Location2
}
{
100.100.100.3
Device3|Location3
}
{
100.100.100.4
Device4|Location4
}
}
我能够正确显示以下输出
for ($n=0; $n < $size; $n++) {
$currentIP=@current_list[$n][0]
DEBUG("DEBUG : Current IP $currentIP exists in current list");
}
输出看起来像
100.100.100.1
100.100.100.2
100.100.100.3
100.100.100.4
MngtIP ---&gt;我们从最新的数据文件中获取 例如,MngtIP是100.100.100.1现在我能够获得&#34; DEBUG:管理IP 100.100.100.1存在于当前列表中的输出&#34;但之后无法删除并打印剩余的列表
翻斗车输出:
$VAR1 = [
'100.100.100.1',
'Device1|India|324|HP|AA|JPJ|Delhi',
'Device1',
'',
'',
''
];
$VAR2 = [
'100.100.100.2',
'Device2|London|564|HP|BB|PLP|Pune',
'Device2',
'',
'',
''
];
$VAR3 = [
'100.100.100.3',
'Device3|Australia|989|HP|CC|MNM|Chennai',
'Device3',
'',
'',
''
];
$VAR4 = [
'100.100.100.4',
'Device4|China|009|HP|DD|BHB|jaipur',
'Device4',
'',
'',
''
];
答案 0 :(得分:1)
看起来你有一个数组数组,而IP地址是内部数组的第一个元素。
我认为你需要的是这个。它将@current_list
复制到@current_list_remove
,删除IP地址匹配$MngtIP
的元素,然后打印出剩余IP列表。
主要问题是您正在尝试使用IP地址字符串索引数组的delete $current_list[$currentIP]
。那不会起作用。
请将use strict
和use warnings
添加到您编写的每个程序的顶部。这个简单的措施会立即提醒你这个错误。
use strict;
use warnings;
my ($Table, $MngtIP);
my @current_list = eval { $Table->invoke("get"); };
my @current_listremove = grep { $_->[0] ne $MngtIP } @current_list;
for my $item (@current_listremove) {
my $ip = $item->[0];
print "$ip\n";
}
<强>更新强>
以下是使用您发布的数据演示上述解决方案,表明其工作方式如上所述
use strict;
use warnings;
my @current_list = (
[ '100.100.100.1', 'Device1|India|324|HP|AA|JPJ|Delhi', 'Device1', '', '', '', ],
[ '100.100.100.2', 'Device2|London|564|HP|BB|PLP|Pune', 'Device2', '', '', '', ],
[ '100.100.100.3', 'Device3|Australia|989|HP|CC|MNM|Chennai', 'Device3', '', '', '', ],
[ '100.100.100.4', 'Device4|China|009|HP|DD|BHB|jaipur', 'Device4', '', '', '', ],
);
my $MngtIP = '100.100.100.3';
my @current_listremove = grep { $_->[0] ne $MngtIP } @current_list;
for my $item (@current_listremove) {
my $ip = $item->[0];
print "$ip\n";
}
<强>输出强>
100.100.100.1
100.100.100.2
100.100.100.4