【发布时间】:2020-11-23 00:26:32
【问题描述】:
我只想解析 main.c 中的标题名称:
#include "foo.h"
#include "bar.h"
#include <stdio.h>
int add(int a,int b){ return a+b; }
int sub(int a, int b){ return a-b; }
int main(){
printf("%i\n",add(1,2));
}
所以我的 perl 脚本如下所示:
#!/usr/bin/perl
open MAIN, $ARGV[0];
@ar = map { /#include "([^"]+)"/ ? $1 : next } <MAIN>;
#this one works (next inside for-loop, not map-loop)
for(<MAIN>){
if(/#include "([^"]+)"/){
push @ar2, $1;
} else {
next;
}
}
print "@ar\n";
print "@ar2\n";
给出错误:
Can't "next" outside a loop block
那么next 可能在map 中吗?如果是这样,如何解决我的问题?
【问题讨论】:
-
提示:始终使用
use strict; use warnings;。还有其他可以改进的地方,但这一点至关重要。 -
for循环中的next之后没有更多操作,因此您实际上根本不需要else块。
标签: arrays loops perl map-function