Perl compare two files and copy lines to new file -
i’m beginner in perl , i’m trying compare 2 files perl. 1 contains list of id’s other 1 has strings contain id’s , more text. want copy lines matching id’s third file, instead of correct strings number. have done wrong?
use strict; use warnings; open ( ids , "<id.txt"); @ids = <ids>; chomp(@ids); close ids; $id = @ids; open ( meta , "<meta.txt"); @metas = <meta>; chomp(@metas); $meta = @metas; open ( out1, ">>", "outtest.txt"); foreach $id (@metas){ print out1 "$meta"."\n"; } close out1; close meta;
try hash variables output:
use strict; use warnings; open ( meta , "<meta.txt"); %idsvalues = (); #create 1 new hash variable while(<meta>) { $line = $_; if($line=~m{<id>(\d+)</id>\s*<string>([^<>]*)</string>}) { $idsvalues{$1} = $2; #store values , text hash variable } } close(meta); #close opened file @values; open ( ids , "<id.txt"); while(<ids>) { $line = $_; if($line=~m/<id>(\d+)<\/id>/i) { #check if value presents in file , push them array variable. push(@values, "ids: $1\tvalues: $idsvalues{$1}") if(defined $idsvalues{$1} ); } } close(ids); #close opened file open ( out1, ">>", "outtest.txt"); print out1 join "\n", @values; #join newline , print output line in output file. close out1; #close opened file
Comments
Post a Comment