in reply to File handles in regular expressions
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:
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.#!/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;
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
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: File handles in regular expressions
by Lotus1 (Vicar) on Oct 18, 2012 at 19:19 UTC | |
by 2teez (Vicar) on Oct 18, 2012 at 23:55 UTC | |
by Lotus1 (Vicar) on Oct 19, 2012 at 02:16 UTC |
In Section
Seekers of Perl Wisdom