while ( !eof FIRST ) { my $i = 0; my ( @first_lats, @first_lons ); while ( !eof FIRST and $i < 10000 ) { $_ = ; if ( $csv->parse( $_ )) { my ( $lat, lon ) = $csv->fields; $first_lats[$i] = $lat; $first_lons[$i] = $lon; $i++; } else { # report csv error } } check_distances( $i, \@first_lats, \@first_lons ); } sub check_distances { my ( $n, $flat, $flon ) = @_; open SECOND, "second.file" or die $!; while () { if ($csv->parse($_)) { my ( $slat, $slon ) = $csv->fields; for ( my $i=0; $i<$n; $i++ ) { # check distance from $slat,$slon to $$flat[$i],$$flon[$i] } } } }