如何替换外部文件中的哈希值?
对于我的程序,我试图用新创建的值替换外部文件中特定哈希值。 外部文件的值以制表符分隔,并且我已从外部文件中读取了哈希值。 我一直在网上四处寻找,这是我能弄清楚如何做到这一点的最接近的方法,但它似乎不起作用。
open(IN, ">>$file") || die "can't read file $file";
while (<IN>) {
print IN s/$hash{$key}/$newvalue/;
}
close (IN)
我不太确定这个公式中缺少什么。
For my program, I'm attempting to replace the value of a specific hash in an external file with a newly created value. The external file has the value tab-delimited from the key, and I had read the hash in from the external file. I've been looking around online, and this is the closest way I could figure out how to do it, yet it doesn't seem to work.
open(IN, ">>$file") || die "can't read file $file";
while (<IN>) {
print IN s/$hash{$key}/$newvalue/;
}
close (IN)
I'm not quite sure what I'm missing in this formula.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Tie::File 可以为您解决此问题。
Tie::File can fix this for you.
http://www.sthomas.net/roberts-perl-tutorial.htm /ch13/Modifying_a_File_with___I
谷歌搜索“$INPLACE_EDIT perl”
http://www.sthomas.net/roberts-perl-tutorial.htm/ch13/Modifying_a_File_with___I
google on "$INPLACE_EDIT perl"
您试图读取和写入同一个文件,这是行不通的。 您必须读取、替换然后写入另一个文件。 之后,如果您确实想要一个文件,则可以将输入文件替换为刚刚编写的文件。
You are trying to read and write to the same file, that is not going to work. You have to read, substitute then write in another file. Afterwards, you can replace the input file by the one you've just written if you really want one file.
这不会很有效,但它应该可以工作,除非我的 perl-fu 很糟糕:
可能有一个命令可以在 perl 中为你做移动,但我不是一个 perl 人。
This won't be efficient, but it should work, unless my perl-fu is bad:
There might be a command to do the move for you in perl, but I'm not a perl guy.