簡體   English   中英

Perl - 在另一個文件中搜索並替換 hash 值

[英]Perl - Search and replace a hash value in another file

我正在創建一個小程序來查找特定的 hash 密鑰並替換其源,但為此我需要找到整個 hash 並將其替換為新的 hash 用戶輸入的值。

我的代碼示例。

print "Please input the smile you would like to edit: ";
$EditSmile = <STDIN>;
print "Please input the text you want to change: ";
$EditText = <STDIN>;

open (IN, "< info.pl") || die("Can not open file: $!");

while (<IN>){
    $var1 =~ s/\'$EditSmile\' => "$smileinfo{"$EditSmile"}"/\'$EditSmile\' => "$EditText"/g;
    print $var1;    
}

其中 hash 值位於文件info.pl中,該文件包含在程序中。

也許你應該閱讀

while ($var1 = <IN>) {
   ...
}

正如上面 Ether 所建議的,另一種選擇是“執行”您的 pl 文件並直接更改 hash。

my $hash = do "info.pl";
if (exists($hash->{$EditSmile})) {
    $hash->{$EditSmile} = $EditText;
}
use Data::Dumper;
print Dumper($hash);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM