這是一個簡單的問題,但找不到任何工作解決方案。我有2個文件,第一個文件包含我感興趣的所有ID,例如「番茄」,「黃瓜」,但也是我不感興趣的,它們在第二個文件中沒有任何價值。第二個文件具有以下數據結構檢索與perl匹配的相同ID的值
tomato red
tomato round
tomato sweet
cucumber green
cucumber bitter
cucumber watery
我需要得到的是包含所有的ID從第二個文件中的所有匹配值的文件,一切選項卡分隔的,就像這樣:
tomato red round sweet
cucumber green bitter watery
我做什麼,到目前爲止是創建一個哈希出的第一個文件的ID的:
while (<FILE>) {
chomp;
@records = split "\t", $_;
{%hash = map { $records[0] => 1 } @records};
}
而這第二個文件:
while (<FILE2>) {
chomp;
@records2 = split "\t", $_;
$key, $value = $records2[0], $records2[1];
$data{$key} = join("\t", $value);
}
close FILE;
foreach my $key (keys %data)
{
print OUT "$key\t$data{$key}\n"
if exists $hash{$key}
}
將一些簡單的解決方案組合所有值匹配相同的ID感謝您! :)
是的。同樣值得一讀'perldoc perldsc',它描述了Perl中的數據結構,包括上面使用的散列數組。 – 2012-08-10 11:47:25
非常感謝,CyberDem0n,它的工作原理應該是:)。謝謝你們,我之前並不熟悉創建哈希數組,現在就來看看吧! – DoubleDecker 2012-08-10 12:06:02