如果只是单词替换的话 s/(\w+)/$replace{$1} || $1/eg 也可以。

2009/11/7 Ruixian Zhu <[email protected]>

> 好的 多谢。
>
> 2009/11/7 Qiang (James) <[email protected]>
>
> 直接 local $/ = undef; 然后打开文件全部读入,直接替换然后重新写入即可。
>>
>> >       $count=0;
>> >       while($count<scalar(@keyword)){
>> >               $_=$C;
>> >               $C =~ s/@keyword[$count]/@replacekeyword[$count]/gi;
>> >               $count++;
>> >       }
>>
>> 这一部分可以用 hash 更省事些。
>>
>> my %k_to_replace = ( k_1 => 'new_k_1', ... );
>>
>> for my $k (keys %k_to_replace) {
>>    $C =~ s/$k/$k_to_replace{$k}/gi;
>> }
>>
>> 另外,代码里声明变量通常要用 my,还要使用 warnings 和 string; 建议你把这
>> 篇文章先看看 http://wiki.perlchina.org/Use_Strict_And_Warnings
>>
>> Qiang
>>
>> Nestle wrote:
>> > 这样是不是文件多个关键字替换的最佳方法
>> >
>> > sub replaceInFile{
>> >       print "Process File:".$_[0]."\n";
>> >       undef $/;
>> >       open TARGET_FILE,$_[0];
>> >         $C = <TARGET_FILE>;
>> >       close TARGET_FILE;
>> >       $/="\n";
>> >
>> >       $count=0;
>> >       while($count<scalar(@keyword)){
>> >               $_=$C;
>> >               $C =~ s/@keyword[$count]/@replacekeyword[$count]/gi;
>> >               $count++;
>> >       }
>> >
>> >       open( FILE, ">$_[0]");
>> >       print FILE $C;
>> >       close(FILE);
>> > }
>> > >
>>
>>
>>
>>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复