Arrays perl如何从数组中拆分字符串并将其保存回

Arrays perl如何从数组中拆分字符串并将其保存回,arrays,perl,Arrays,Perl,我曾尝试从数组中拆分字符串,并用perl将其保存回去,但它无法很好地拆分。 我已将数据存储到数组@nameInt。 数据: 代码: 我试图拆分并保存到新数组@sepInt中,但当我试图打印出来时,它显示错误 print "name interface: ".$sepInt[0][0]." | vlan id: ".$sepInt[0][1]; Can't use string ("ge-1/1/5") as an ARRAY ref while "strict refs" in use

我曾尝试从数组中拆分字符串,并用perl将其保存回去,但它无法很好地拆分。 我已将数据存储到数组@nameInt。 数据:

代码:

我试图拆分并保存到新数组@sepInt中,但当我试图打印出来时,它显示错误

print "name interface: ".$sepInt[0][0]." | vlan id: ".$sepInt[0][1];
    Can't use string ("ge-1/1/5") as an ARRAY ref while "strict refs" in use
预期:

name interface: ge-1/1/2 | vlan id: 552
name interface: ge-1/1/2 | vlan id: 561
name interface: ge-1/1/2 | vlan id: 562
...
so on

在该循环的每次迭代中:

foreach my $interface(@nameInt){
    @sepInt = split /[.]/, $interface;
}
将@sepInt替换为新拆分的行@sepInt始终是字符串数组。所以当你到达

print "name interface: ".$sepInt[0][0]." | vlan id: ".$sepInt[0][1];
    Can't use string ("ge-1/1/5") as an ARRAY ref while "strict refs" in use
$sepInt[0]是字符串,不是数组


您的意思是在那里使用$nameInt[0][0]吗?

此循环的每次迭代:

foreach my $interface(@nameInt){
    @sepInt = split /[.]/, $interface;
}
将@sepInt替换为新拆分的行@sepInt始终是字符串数组。所以当你到达

print "name interface: ".$sepInt[0][0]." | vlan id: ".$sepInt[0][1];
    Can't use string ("ge-1/1/5") as an ARRAY ref while "strict refs" in use
$sepInt[0]是字符串,不是数组


您是想在那里使用$nameInt[0][0]吗?

欢迎使用Perl中的高阶函数

map {split /[.]/, $_} @nameInt
内置的功能图可以帮助您在树的上方看到森林。换句话说,它帮助您将注意力集中在您想要做的事情上,而不是集中在中间簿记值上,例如$interface和@sepInt

…嗯,也许你想要的是一个元素对的列表

map {[split /[.]/, $_]} @nameInt

欢迎使用Perl中的高阶函数

map {split /[.]/, $_} @nameInt
内置的功能图可以帮助您在树的上方看到森林。换句话说,它帮助您将注意力集中在您想要做的事情上,而不是集中在中间簿记值上,例如$interface和@sepInt

…嗯,也许你想要的是一个元素对的列表

map {[split /[.]/, $_]} @nameInt
使用数组的数组:

foreach my $interface(@nameInt){
    push @sepInt, [ split /[.]/, $interface ];
}
使用数组的数组:

foreach my $interface(@nameInt){
    push @sepInt, [ split /[.]/, $interface ];
}

鉴于您正在创建一个数组,而不是一个数组数组,我认为您试图做的是:

my @array = qw (ge-1/1/2.552 ge-1/1/2.561 ge-1/1/2.562 ge-1/1/2.580 ge-1/1/2.582 ge-1/1/2.590 ge-1/1/2.592 ge-1/1/2.640 ge-1/1/2.642 ge-1/1/2.644 ge-1/1/2.650);

foreach (@array){
    my @sepInt = split(/[.]/);
    print "name interface: $sepInt[0] | vlan id: .$sepInt[1]\n"
}
产出:

name interface: ge-1/1/2 | vlan id: .552
name interface: ge-1/1/2 | vlan id: .561
name interface: ge-1/1/2 | vlan id: .562
name interface: ge-1/1/2 | vlan id: .580
name interface: ge-1/1/2 | vlan id: .582
name interface: ge-1/1/2 | vlan id: .590
name interface: ge-1/1/2 | vlan id: .592
name interface: ge-1/1/2 | vlan id: .640
name interface: ge-1/1/2 | vlan id: .642
name interface: ge-1/1/2 | vlan id: .644
name interface: ge-1/1/2 | vlan id: .650

鉴于您正在创建一个数组,而不是一个数组数组,我认为您试图做的是:

my @array = qw (ge-1/1/2.552 ge-1/1/2.561 ge-1/1/2.562 ge-1/1/2.580 ge-1/1/2.582 ge-1/1/2.590 ge-1/1/2.592 ge-1/1/2.640 ge-1/1/2.642 ge-1/1/2.644 ge-1/1/2.650);

foreach (@array){
    my @sepInt = split(/[.]/);
    print "name interface: $sepInt[0] | vlan id: .$sepInt[1]\n"
}
产出:

name interface: ge-1/1/2 | vlan id: .552
name interface: ge-1/1/2 | vlan id: .561
name interface: ge-1/1/2 | vlan id: .562
name interface: ge-1/1/2 | vlan id: .580
name interface: ge-1/1/2 | vlan id: .582
name interface: ge-1/1/2 | vlan id: .590
name interface: ge-1/1/2 | vlan id: .592
name interface: ge-1/1/2 | vlan id: .640
name interface: ge-1/1/2 | vlan id: .642
name interface: ge-1/1/2 | vlan id: .644
name interface: ge-1/1/2 | vlan id: .650


我猜你的意思是@sepInt=map{[split/[.]/,$\]}@nameInt@Y是的,我的意思是。@ajm475du:如果我们得到空值或未定义的值,我们如何将其更改为其他字符串?我猜您的意思是@sepInt=map{[split/[.]/,$\u]}@nameInt@ysth是的,我的意思是。@ajm475du:如果我们得到空值或未定义的值,我们如何将其更改为其他字符串?我不是说它会工作,我只是想弄清楚他想做什么:-我不是说它会工作,我只是想弄清楚他想做什么:-push@sepInt,[split/.]/,$interface]。。。是吗?我已经试过了,但当我打印$sepInt[0][0]时,仍然无法使用字符串ge-1/0/0。push@sepInt,[split/[.]/,$interface]。。。是吗?我已经试过了,但当我打印$sepInt[0][0]时仍然出现错误,无法使用字符串ge-1/0/0。谢谢朋友,如果它为空,就像没有的ge-1/1/2一样,我收到了错误消息,在串联中使用了未初始化的值。或字符串。是因为我用的是严格的吗?是因为你要打印一些不存在的东西。如果没有。在元素中不能有$sepInt[0]和$sepInt[1]我们如何将未初始化的值更改为其他值?如果没有。在元素?@rabka中,将字符串拆分为@sepInt后,您可以执行$sepInt[1]| |=none以确保其中存在有效的字符串值。谢谢朋友,如果它为null,如ge-1/1/2,则在串联中使用未初始化的值,我会收到错误消息。或字符串。是因为我用的是严格的吗?是因为你要打印一些不存在的东西。如果没有。在元素中不能有$sepInt[0]和$sepInt[1]我们如何将未初始化的值更改为其他值?如果没有。在元素?@rabka中,将字符串拆分为@sepInt后,可以执行$sepInt[1]| |=none以确保其中存在有效的字符串值。