Link to home
Start Free TrialLog in
Avatar of warrior32
warrior32Flag for Afghanistan

asked on

Using File::Tie to open file grab strng and search for it on different file

In perl how can use File::Tie to to open file1 read the first line and search for that string in file2, if found print out "found string" if not found print out "Not found".  Then continue to the second string in file1 searching for it in file2.  So on so forth until no more lines in file1.
I don't want to read the whole file to memory because both of these files might get very large in size.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam314
Adam314

I don't see any need for Tie::File for this, you can just read the file one line at a time, and search for the string.
open(my $fh1, '<file1.txt') or die "could not open file1: $!\n";
open(my $fh2, '<file2.txt') or die "Could not open file2: $!\n";
while(my $s1=<$fh1>) {
	chomp $s1;
	seek $fh2,0,0;
	my $found=0;
	while(<$fh2>) {
		next unless /$s1/;
		print "$s1: found string\n";
		$found=1;
		last;
	}
	print "$s1: Not found\n" unless $found;
}
close($fh1);
close($fh2);

Open in new window