#!/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;