这是我的输入C文件
/**
********************************************************************************
* @fn ChainCtrlSetJpgSnapshotFile
* @brief
* @param[in ]
* @return
********************************************************************************
*/
eErrorT ChainCtrlSetJpgSnapshotFile(ChainCtrlT* pChainCtrl, RouteListItemT* pRoute, char * dst_chain, char *jpg_file_path)
{
...
}
/**
********************************************************************************
* @fn ChainCtrlSetBgFile
* @brief
* @param[in ]
* @return
********************************************************************************
*/
eErrorT ChainCtrlSetBgFile(ChainCtrlT* pChainCtrl, RouteListItemT* pRoute, char * dst_chain, char *bg_file_path)
{
...
}我的Perl代码
use strict;
use warnings;
use vars qw(@temp $index);
open(my $FILE, "< a.c") or die $!; ;
my @arr = <$FILE>;
my $pos = 0;
foreach(@arr){
$pos++;
if ($_ =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg) {
my $arg = $1;
$index = $pos;
my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
@temp = map ' * @param[in/out] '."$_\n", @arr;
}
}
$arr[$index - 5] = "";
splice @arr,$index-4,0,@temp;
print @arr;
close($FILE);--我得到的错误输出
/**
********************************************************************************
* @fn ChainCtrlSetJpgSnapshotFile
* @brief
* @param[in ]
* @return
********************************************************************************
*/
eErrorT ChainCtrlSetJpgSnapshotFile(ChainCtrlT* pChainCtrl, RouteListItemT* pRoute, char * dst_chain, char *jpg_file_path)
{
...
}
/**
********************************************************************************
* @fn ChainCtrlSetBgFile
* @brief
* @param[in/out] pChainCtrl
* @param[in/out] pRoute
* @param[in/out] dst_chain
* @param[in/out] bg_file_path
* @return
********************************************************************************
*/
eErrorT ChainCtrlSetBgFile(ChainCtrlT* pChainCtrl, RouteListItemT* pRoute, char * dst_chain, char *bg_file_path)
{ ....
}我试图将每个函数的参数的名称添加到它上面的注释部分。
我的代码只适用于最后一个函数定义,但我需要它来处理C文件中的所有函数。
我想我在splice上犯了一个错误,但是在foreach循环中使用splice会使它无限期地运行,没有输出。
发布于 2013-10-15 13:47:59
我花了半个小时看你的程序,下面是正确的answer.It似乎是因为一个简单的逻辑error.So,如果你喜欢,请给我一些分数。
use strict;
use warnings;
use vars qw(@temp $index);
open(my $FILE, "< a.c") or die $!; ;
my @arr = <$FILE>;
my $pos = 0;
foreach(@arr){
$pos++;
if ($_ =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg) {
my $arg = $1;
$index = $pos;
my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
@temp = map ' * @param[in/out] '."$_\n", @arr;
$arr[$index - 5] = "";
splice @arr,$index-4,0,@temp;
}
}
print @arr;
close($FILE);发布于 2013-10-04 09:59:40
包含splice的三行代码写在for循环和if之外。因此,它们只执行一次,就在文件关闭之前。
将for循环和if的两个大括号移到close语句之前。
循环中的拼接不断地运行,因为它每次在循环周围向数组中添加更多行。
代码编写方式的一个根本问题是,数组@arr正在由foreach (@arr)循环处理,而额外的元素正在被拼接到循环中的数组中。Perl擅长在编写这样的代码时做“正确的事情”。您的代码通过计数将索引保存到数组(即$pos和$index)中。在索引引用的元素之前将元素拼接到数组中时,不会调整这些计数。
https://stackoverflow.com/questions/19177826
复制相似问题