open(FILE, ">$file"); 请改为 open(FILE, "+<$file");



2009/1/18 伟子 <[email protected]>

> 有一个指定文件names.txt,共有50行,均为历届总统人名。如:
> Bill Clinton
> Goerge Bush
> ...
>
> 在另一个目录下,有1000个文件(从file0.txt到file999.txt),每个文件的内容可能会包含上述文件中的人名。我想把这1000个
> 文件中任意有上述人名的替换为"Barack Obama"(与时俱进,哈哈)。
> 下面是我的程序,但输出总是空文件。我是刚刚开始学perl,希望能够得到指点,谢谢!
>
> #!/usr/bin/perl
>
>
> # Open names.txt file
> open(NAMES, "<names.txt");
>
> # Get the handle of all files to be modified
> opendir DIR, "newspaper";
> @files = readdir DIR;
> closedir DIR;
>
> foreach $file (@files)
> {
>    open(FILE, ">$file");
>    while(<NAMES>)
>    {
>        @F = split /\s+/;
>        print FILE map { s/$F[0]\s$F[1]/John Smith/g; $_; };
>        close FILE;
>    }
>    close FILE;
> }
>
> close NAMES;
> >
>

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

回复