Hi Vikasdawar,
Please for you open function check if it works or display an error message or you use autodie qw(open close)
Please, try the following below:

#!/usr/bin/perl use warnings; use strict; use Tie::File; tie my @array_file, 'Tie::File', "file1.txt" or die "can't tie file: $ +!"; my $matched_lines = ''; open my $fh, '>', "file3.txt" or die "can't open file: $!"; open my $fh2, '<', "file2.txt" or die "can't open file: $!"; while ( defined( my $line = <$fh2> ) ) { chomp $line; foreach my $match (@array_file) { if ( $match eq $line and $match ne "") { $matched_lines .= $match.$/; next; } } } print {$fh} $matched_lines; close $fh2 or die "can't close file:$!"; close $fh or die "can't close file:$!"; untie @array_file;
NOTE: The code above thus work,(or should work) but might not be so effiecient if it has a VERY VERY LARGE files to compare.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: File handles in regular expressions by 2teez
in thread File handles in regular expressions by vikasdawar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.