my %aircraft_ids; for my $row ( INPUT_ROWS ) { my ($row_id,$a_id,@row_fields) = split(',', $row); if ( exists( $aircraft)ids{$a_id} ) { my $prior_fields = $aircraft_ids{$a_id}; # do calcs here comparing current row data to priors } # and save these data points for the next time the ID is seen. $aircraft_ids{$a_id} = \@row_fields; }