這是我當前的腳本,嘗試將file_all.txt
中的單詞與file2.txt
中的單詞進行比較。它應該打印出file_all
中不在file2
中的任何單詞。Perl:比較兩個文件中的單詞
我需要將它們格式化爲每行一個字,但這不是更緊迫的問題。
我是新來的Perl ...我得到C和Python更多,但這是有點棘手,我知道我的變量分配關閉。
use strict;
use warnings;
my $file2 = "file_all.txt"; %I know my assignment here is wrong
my $file1 = "file2.txt";
open my $file2, '<', 'file2' or die "Couldn't open file2: $!";
while (my $line = <$file2>) {
++$file2{$line};
}
open my $file1, '<', 'file1' or die "Couldn't open file1: $!";
while (my $line = <$file1>) {
print $line unless $file2{$line};
}
編輯:哦,它應該忽略的情況...就像餡餅和PIE比較時一樣。並刪除撇號
這是我收到的錯誤:
"my" variable $file2 masks earlier declaration in same scope at absent.pl line 9. "my" variable $file1 masks earlier declaration in same scope at absent.pl line 14. Global symbol "%file2" requires explicit package name at absent.pl line 11. Global symbol "%file2" requires explicit package name at absent.pl line 16. Execution of absent.pl aborted due to compilation errors.
看起來你是在正確的軌道上。問題是什麼? – mob
如果我嘗試運行它,我會在absent.pl第6行獲得散列分配中奇數個元素。 absent.pl第7行散列分配中元素的奇數個數。 無法打開文件2:沒有這樣的文件或目錄在absent.pl第9行。 – user3295674
我不確定這是什麼意思,因爲我是新來的Perl(對不起,如果這是一個愚蠢的問題!) – user3295674