以下代碼逐行讀取defines.c文件並將所有"#define"
指令存儲在散列中(定義了名稱及其替換文本)。然後,如果在連續行上查找使用情況,則將它們替換它們。Perl替換:無法用變量perl替換正則表達式
如果使用"#define"
指令,那麼替換時出現問題。它正確地將所有#defines
存儲在散列中。
我是一個perl和正則表達式的初學者,無法指出愚蠢的事情,爲什麼它不工作。
任何幫助?
#!/usr/bin/perl -w
use strict;
my %definesHash;
open(FILE, "defines.c") || die "Cannot open $!\n";
open(OUT, ">defines.i") || die "Cannot open $!\n";
while (<FILE>)
{
my $line = $_;
if ($line =~ /#define\s+/)
{
$line =~ s/#define\s+//g;
if ($line =~ /\b([\w]+)\b\s+/)
{
my $define = $1;
$line =~ s/\b[\w]+\b\s+//;
$definesHash{$define} = "";
if($line =~ /\s*(.*)\s*/)
{
$definesHash{$define} = $1;
}
}
print OUT $_;
}
else
{
my($def, $replace);
while (($def, $replace) = each(%definesHash))
{
print " $def => $replace \n";
if ($line =~ /$def/)
{
$line =~ s/$def/$replace/g; #****** Some Problem Here, But What? ********
}
}
print OUT $line;
}
}
close(FILE);
close(OUT);
defines.c
#include <stdio.h>
#define VALUE 10
#define PLACE (20 + 0)
#define NUM (VALUE + 10)
int main()
{
int num;
num = NUM + 25 + NUM + PLACE;
return 0;
}
Expected Output: **defines.i**
#include <stdio.h>
#define VALUE 10
#define PLACE (20 + 0)
#define NUM (VALUE + 10)
int main()
{
int num;
num = (10 + 10) + 25 + (10 + 10) + (20 + 0);
// or
num = (VALUE + 10) + 25 + (VALUE + 10) + (20 + 0);
return 0;
}
我知道這個問題是在替代正如我指出的評論。在替換之後,我可以看到原始行變成亂碼,而散列中的內容似乎是正確的。
它與替代變量的用法?
你可以給出例子和預期的輸出。 – Sobrique
你的腳本適合我。我得到了預期的結果。 – Toto
您的程序提供了預期的輸出並正常工作。你有什麼問題?你無法獲得預期的產出? – Praveen