我想在一個巨大的文件中替換字符串包含許多行。如何使用散列將「x」和「y」換成「y」和「x」
FILE.TXT
line1: "X" = 5.5; "Y" = 7.5; "Z" = 9.0;
line2: "V" = 66; "Y" = 3; "U" = 11.0;
等等...
替身哈希值(%rhash)包含地圖信息
$rhash{"X"} = "Y";
$rhash{"Y"} = "X";
$rhash{"Z"} = "A";
$rhash{"V"} = "B";
$rhash{"U"} = "C";
當我試圖
while (($cur, $cng) = each(%rhash)) {
$line =~ s/\Q"$cur"\E/\"$cng\"/g;
}
第1行是int變化Ø無論
"X" = 5.5; "X" = 7.5; "A" = 9.0;
或
"Y" = 5.5; "Y" = 7.5; "A" = 9.0;
但正確的改變是
"Y" = 5.5; "X" = 7.5; "A" = 9.0;
我怎麼能做到這一點..
感謝您的幫助......
注:哈希鍵不僅是文字。鍵可以是任何匹配的模式(\ S +) – AKS