in reply to compare two text file line by line, how to optimise
perltidy can beuatify your code in a blink but no one but you can put more effort when choosing variable names. I tell this because occured to me, and passed some weeks, i never could re-understand my own code. Even more important if someone else looks at your code: a well choosed name can do the differnce and few keystroke more are worth in the distance.
Anyway if you just want intersection of two lists is faq, see How can I find the union/difference/intersection of two arrays?. Using exists on a hash list of keys can be usefull.
If i've understood your question you can try something like
# untested while (<$first_fh>){ chomp; my %infirst; # every word become a key of temporary hash map{$infirst{$_}++} split ' ',$_; my $ref_line = <$second_fh>; chomp $ref_line; print grep {exists $infirst{$_}} split ' ',$ref_line; die "$file_one exhausted!" if eof($first_fh); die "$file_two exhausted!" if eof($second_fh); }
For large files be awere to never evaluate <$fh> il list context: see recent Re: How to read in large files
L*
|
|---|