次のことを行うスクリプトを書きたいと思います: ユーザーは .txt ファイルをインポートすることを選択できます (このためにコードを書きました)(こちら$list1
)。このファイルは、各行に名前を持つ 1 つの列のみで構成されます。ユーザーが空ではないファイルをインポートした場合、別のファイル (ここで$file2
は) の列の名前をインポートされたファイルの名前と比較します。一致する場合は、この元のファイル ( ) の行全体を$file2
新しいファイル ( $filter1
) に配置する必要があります。
これは私がこれまでに持っているものです:
my $list1;
if (prompt_yn("Do you want to import a genelist for filtering?")){
my $genelist1 = prompt("Give the name of the first genelist file:\n");
open($list1,'+<',$genelist1) or die "Could not open file $genelist1 $!";
}
open(my $filter1,'+>',"filter1.txt") || die "Can't write new file: $!";
my %hash1=();
while(<$list1>){ # $list1 is the variable from the imported .txt file
chomp;
next unless -z $_;
my $keyfield= $_; # this imported file contains only one column
$hash1{$keyfield}++;
}
seek $file2,0,0; #cursor resetting
while(<$file2>){ # this is the other file with multiple columns
my @line=split(/\t/); # split on tabs
my $keyfield=$line[2]; # values to compare are in column 3
if (exists($hash1{$keyfield})){
print $filter1 $_;
}
}
このスクリプトを実行すると、出力filter1.txt
が空になります。列間に間違いなく一致があるため、これは正しくありません。