in reply to comparing arrays

Anonymous Monk,
Given that I have no idea how large your files are, reading everything into memory might not be feasible. OTOH, looping through file2 as many times as there are entries in file1 may also be too time consuming. I have comprimised by caching the offset in file2.
#!/usr/bin/perl use strict; use warnings; my $file_1 = $ARGV[0] || 'file1.txt'; my $file_2 = $ARGV[1] || 'file2.txt'; open (FILE1, '<', $file_1) or die "Unable to open $file_1 for reading +: $!"; open (FILE2, '<', $file_2) or die "Unable to open $file_2 for reading +: $!"; my %offset = ( _pos => 0 ); while ( <FILE1> ) { chomp; if ( defined $offset{ $_ } ) { seek FILE2, $offset{ $_ }, 0; print scalar <FILE2>; next; } else { seek FILE2, $offset{_pos}, 0; my $pos = tell FILE2; while ( my $line = <FILE2> ) { my ($col1) = $line =~ /^(\d+)/; $offset{ $col1 } = $pos; $pos = tell FILE2; if ( $col1 eq $_ ) { print $line; $offset{_pos} = $pos; last; } } } }
This is fully functional and should be a comprimise between speed and memory.

Cheers - L~R

Update: Added optimization so that each line from file 2 is read a maximum of 2 times