in reply to comparing arrays
This is fully functional and should be a comprimise between speed and memory.#!/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; } } } }
Cheers - L~R
Update: Added optimization so that each line from file 2 is read a maximum of 2 times
|
|---|