【问题标题】:perl how to split string from array and save it backperl如何从数组中拆分字符串并将其保存回来
【发布时间】:2013-12-13 00:18:08
【问题描述】:

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

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 my $interface(@nameInt){
    @sepInt = split /[.]/, $interface;
}

我尝试拆分并保存到新数组 (@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

【问题讨论】:

    标签: arrays perl


    【解决方案1】:

    看到您正在创建一个数组,而不是数组数组,我认为您正在尝试做的是:

    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
    

    【讨论】:

    • 谢谢朋友,如果它为空,比如没有“.”的 ge-1/1/2,我收到错误消息“在连接 (.) 或字符串中使用未初始化的值”。是因为我使用严格吗?
    • 这是因为您要打印一些不存在的东西。如果您的元素中没有.,则不能有$sepInt[0]$sepInt[1]
    • 我们如何将未初始化的值更改给其他人?如果元素中没有.
    • @rabka 将字符串拆分为@sepInt 后,您可以执行$sepInt[1] ||= "none" 以确保那里有一个有效的字符串值。
    【解决方案2】:

    欢迎使用 Perl 中的高阶函数!

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

    内置函数map 帮助您爬到树上,看到森林。换句话说,它可以帮助您将注意力集中在您想要做的事情上,而不是像您的 $interface@sepInt 这样的中间簿记值。

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

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

    【讨论】:

    • 我猜你的意思是@sepInt = map { [ split /[.]/, $_ ] } @nameInt;?
    • @ysth 是的,我是这个意思。
    • @ajm475du : 如果我们得到 null 或 undefined 值,我们如何将其更改为其他字符串?
    【解决方案3】:

    使用数组数组:

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

    【讨论】:

    • push @sepInt, [ split /[.]/, $interface ]; ... 是吗?
    • 我试过了,打印 $sepInt[0][0] 时仍然报错 Can't use string ("ge-1/0/0")。
    【解决方案4】:

    在此循环的每次迭代中:

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

    您将@sepInt 替换为新拆分的行。 @sepInt 始终是一个字符串数组。所以当你到达​​p>

    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]吗?

    【讨论】:

    • 我并不是说它会起作用,我只是想弄清楚他想要做什么:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2016-08-15
    • 1970-01-01
    • 2020-05-07
    • 2013-05-28
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多