in reply to how to reset the input operater in while loop?
Probably you are trying to do the wrong thing. You should almost never need to reread a file in a nested loop like that. Most likely what you need to do is read file2 into a hash then use hash lookups to check for matches in file1. Something like:
use strict; use warnings; my $data1 = <<DATA; 1 a 2 b 3 c DATA my $data2 = <<DATA; 4 d 1 x DATA my %data2Hash; open my $infile2, '<', \$data2; while (<$infile2>) { chomp; my ($key, $tail) = split ' ', $_, 2; $data2Hash{$key} = $tail; } close $infile2; open my $infile1, '<', \$data1; while (<$infile1>) { chomp; my ($key, $tail) = split ' ', $_, 2; if (exists $data2Hash{$key}) { print "Matched $key: $data2Hash{$key}, $tail\n"; } }
Prints:
Matched 1: x, a
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to reset the input operater in while loop?
by lightoverhead (Pilgrim) on Sep 30, 2008 at 07:06 UTC | |
by GrandFather (Saint) on Sep 30, 2008 at 09:34 UTC | |
by graff (Chancellor) on Oct 01, 2008 at 04:13 UTC | |
by graff (Chancellor) on Oct 01, 2008 at 04:27 UTC |