1

I'm looking for some assistance on a script which can go through the lines of an array, printing them to the screen and stopping when the script detects a specific character, in this case the ! mark. I have tried using the foreach statement but ain't getting any success...

Example of the Array (@lines) contents is:

ip vrf test
rd 2856:10000331
export map SetAltMgmtRT
route-target export 2856:10000331
route-target import 2856:10000331
maximum routes 1000 75
!

The script I have so far is:

elsif ( $action eq "show_vrf" ) {
            my $cmd = "show run | begin <VRF_NAME>";
            $cmd = $cmd . " | i $include" if($include) ;
            my @lines = $s->cmd(String => $cmd,
                            Prompt  => "/$enableprompt/",
                            Timeout => 10);
            foreach $lines (@lines) {
                    <statement, this is where I am stuck>
            }
            print $lines;

Any help would be appreciated :)

4

3 回答 3

4

停止的标准是什么?有感叹号还是只有一个感叹号?还是只是以感叹号开头的一行?

您还lines需要整理好几件事情。

my $output;
foreach my $line (@lines) {
    last if $line =~ m/^!/; # leave loop if line starts with an exclamation mark
    $output .= $line;
}
print $output;

对于您在下面的评论中的额外要求(数据有多个感叹号),您需要这样的东西:

use Data::Dumper;
my @output; # assign output chunks into an array
my $i = 0;
foreach my $line (@lines) {
    if ($line =~ m/^!/) {
        $i++;
        next;
    }
    $output[$i] .= $line;
}
print Dumper(\@output);
于 2013-07-18T10:00:34.623 回答
2

如果行匹配,则中断!

last if ($line =~ /!/);
于 2013-07-18T09:58:04.050 回答
1
for(@lines){
last if(/\!$/);# this will be true if there is an Exclamation mark at the end of line
print $_
}
于 2013-07-18T10:28:31.370 回答