我想在文件中讀入一些符號,如「!」和「^」,並希望在將它們與另一行中的其他字符串進行比較之前將其刪除。如果兩個字符串在刪除符號後都相同,我想將它們存儲在另一個名爲「common」的散列中。 例如... FILEA:比較2個哈希中的2個處理密鑰
hello!world
help?!3233
oh no^!!
yes!
FILEB: 「!」
hello
help?
oh no
yes
在這種情況下,FILEA和FILEB應該是相同的,因爲我比較字符,直到達到地方或「^」出現。 我用下面的代碼讀取文件:
open FILEA, "< script/".$fileA or die;
my %read_file;
while (my $line=<FILEA>) {
(my $word1,my $word2) = split /\n/, $line;
$word1 =~ s/(!.+)|(!.*)|(\^.+)|(\^.*)//;#to remove ! and^
$read_file{$word1} = $word1;
}
close(FILEA);
我在哈希打印出來的鑰匙,它顯示了正確的結果(即其轉換FILEA爲「你好,幫忙?哦不,是)但是,當我做FILEA和FILEB的比較使用下面的代碼,它總是失敗。
while(($key,$value)=each(%config))
{
$num=keys(%base_config);
$num--;#to get the correct index
while($num>=0)
{
$common{$value}=$value if exists $read_file{$key};#stored the correct matches in %common
$num--;
}
}
我想測試我的替代和使用下面的示例2串之間的比較和它的作品。我不不知道爲什麼它不能將字符串讀取到文件中的散列表中。
use strict;
use warnings;
my $str="hello^vsd";
my $test="hello";
$str =~ s/(!.+)|(!.*)|(\^.+)|(\^.*)//;
my %hash=();
$hash{$str}=();
foreach my $key(keys %hash)
{
print "$key\n";
}
print "yay\n" if exists $hash{$test};
print "boo\n" unless exists $hash{$test};
這兩個文件都可以有不同數量的文本行,並且搜索時文本的行不必相同。即。 「哦,不」可以在「你好」之前。
輸出,你可以假設,在文件中的每一行應該只相比,在文件B對應的線?並且文件A和文件B具有相同的行數? – ardnew 2012-02-23 03:20:10
否.FileA和FileB可以具有不同的行數,並且行不必是相同的順序。 – Sakura 2012-02-23 04:05:07