在Perl中填充和迭代数组散列
我想做的是根据设备IP组织一组配置参数 对于每个IP,都有一个索引列表(在Perl中填充和迭代数组散列,perl,data-structures,reference,Perl,Data Structures,Reference,我想做的是根据设备IP组织一组配置参数 对于每个IP,都有一个索引列表($row),我必须为该特定IP生成配置 (这些索引在此上下文中是我的“数据”。不要将它们视为索引,它们是我必须放在那里的东西,然后在配置生成期间获取它们) 我需要“分组”的数据可能看起来像这样 $routerIP $row (an index to a global hash containing various details) 10.10.10.10 2 10.10.10.12 1 10.10.10.10 0
$row
),我必须为该特定IP生成配置
(这些索引在此上下文中是我的“数据”。不要将它们视为索引,它们是我必须放在那里的东西,然后在配置生成期间获取它们)
我需要“分组”的数据可能看起来像这样
$routerIP $row (an index to a global hash containing various details)
10.10.10.10 2
10.10.10.12 1
10.10.10.10 0
10.10.10.12 3
所以我想得到(我确实想要,看看打印输出!)
我将%ipGroup
填入一个循环,该循环将遍历数据:
my $routerIP = #code to get the ip
my $ind = $ipGroup{$routerIP}[0]+1;
$ipGroup{$routerIP}[0] = $ind;
$ipGroup{$routerIP}[$ind] = $row;
在数组中的第0个元素中为每个IP快速存储“当前索引”$ind
对于最新的$ind
放入当前的$row
。以这种方式在该$routerIP
键的数组中前进
如何读取不同子例程中的组(%isGroup是全局的):
问题是:
尽管它对每个routerIP进行迭代,但它不会遍历哈希中最后一个路由器的所有@行
因此,根据上面的数据,我可以为10.10.10.10获得一个正确的配置,包括$row=2
和$row=0
但对于10.10.10.12,id仅获取$row=3
的配置
为什么它会迭代一个键的所有行,而不是另一个,为每个哈希键打印数组我看到它们是不同的,我不只是在两个$hash值中引用1个数组。
对于所有的循环选项以及引用和取消引用数据结构初始化,我完全搞糊涂了
填充时的代码(填充$row
时命名为$hashId
)
print“更新的路由器:$routerIP索引:$ind hashId:$hashId
”代码>
填充完成后的数据转储:
$VAR1 = {
'10.10.10.12' => [
2,
'2',
'3'
],
'10.10.10.10' => [
3,
'0',
'1',
'4'
]
};
在代码的内部while循环中:
print“在循环中:$routerIP index:$index hashId:
”代码>
我看到了不一致的地方,但我不知所措,为此花了好几个小时。我想出了这个。它使用了所有引用,并且非常干净
#!/usr/bin/perl
use strict;
# Setup a reference to a HASH
my $ipGroup = {};
# Populate your structure with __DATA__
foreach my $item (<DATA>) {
$item =~ m/([0-9\.]+)\s(.*)/;
# Push the "row" onto an array dynamically
push @{$ipGroup->{$1}}, $2;
}
foreach my $routerIP ( keys %{$ipGroup} ) {
foreach my $row ( @{$ipGroup->{$routerIP}} ) {
print "Doing stuff to $routerIP with index [ $row ] \n";
}
}
我想说,问题几乎肯定出在构建散列的代码中。这看起来像是在while
循环后最后读取的数据项未被刷新的典型情况。请显示更多导入数据的代码。请同时提供散列的转储:use Data::Dumper;打印转储程序\%ipGroup代码>是的,请耐心听我说,我会尽可能多地添加Perl数据结构食谱中的示例。事实上,我已经快速完成了这项任务,没有时间,仍然在掌握Perl。这就是我想要做的。是的,我现在就开始尝试,再次使用Perl语法向导,爱和恨同时存在啊哈。很乐意帮忙。祝你度过愉快的一天。在做任何其他事情之前,你应该在任何时候进行匹配时测试结果正则表达式。您可以在源文件中有一行与正则表达式不匹配,然后将前一行的$2再次推入@{$ipGroup->{$1}。
updated router: 10.10.10.10 index: 1 hashId: 0
updated router: 10.10.10.10 index: 2 hashId: 1
updated router: 10.10.10.12 index: 1 hashId: 2
updated router: 10.10.10.12 index: 2 hashId: 3
updated router: 10.10.10.10 index: 3 hashId: 4
$VAR1 = {
'10.10.10.12' => [
2,
'2',
'3'
],
'10.10.10.10' => [
3,
'0',
'1',
'4'
]
};
in loop: 10.10.10.12 index: 1 hashId: 2
in loop: 10.10.10.12 index: 2 hashId: 3
in loop: 10.10.10.12 index: 3 hashId:
in loop: 10.10.10.10 index: 1 hashId: 0
#!/usr/bin/perl
use strict;
# Setup a reference to a HASH
my $ipGroup = {};
# Populate your structure with __DATA__
foreach my $item (<DATA>) {
$item =~ m/([0-9\.]+)\s(.*)/;
# Push the "row" onto an array dynamically
push @{$ipGroup->{$1}}, $2;
}
foreach my $routerIP ( keys %{$ipGroup} ) {
foreach my $row ( @{$ipGroup->{$routerIP}} ) {
print "Doing stuff to $routerIP with index [ $row ] \n";
}
}
__DATA__
10.10.10.10 2
10.10.10.12 1
10.10.10.10 0
10.10.10.12 3