私はperlの初心者で、2つのファイルをperlと比較しようとしています。1つにはIDのリストが含まれ、もう1つにはIDとその他のテキストを含む文字列が含まれます。IDが一致する行を3番目のファイルにコピーしたいのですが、正しい文字列の代わりに数字しか取得できません。私は何を間違えましたか?
use strict;
use warnings;
open ( IDS , "<id.txt");
my @ids = <IDS>;
chomp(@ids);
close IDS;
my $id = @ids;
open ( META , "<meta.txt");
my @metas = <META>;
chomp(@metas);
my $meta = @metas;
open ( OUT1, ">>", "outtest.txt");
foreach $id (@metas){
print OUT1 "$meta"."\n";
}
close OUT1;
close META;
ハッシュ変数を試して出力を取得します。
use strict;
use warnings;
open ( META , "<meta.txt");
my %idsValues = (); #Create one new HASH Variable
while(<META>)
{
my $line = $_;
if($line=~m{<id>(\d+)</id>\s*<string>([^<>]*)</string>})
{
$idsValues{$1} = $2; #Store the values and text into the HASH Variable
}
}
close(META); #Close the opened file
my @Values;
open ( IDS , "<id.txt");
while(<IDS>)
{
my $line = $_;
if($line=~m/<id>(\d+)<\/id>/i)
{
#Check if the value presents in the file and push them into ARRAY Variable.
push(@Values, "IDS: $1\tVALUES: $idsValues{$1}") if(defined $idsValues{$1} );
}
}
close(IDS); #Close the opened file
open ( OUT1, ">>", "outtest.txt");
print OUT1 join "\n", @Values; #Join with newline and Print the output line in the output file.
close OUT1; #Close the opened file
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加