use strict; use warnings; use Data::Dumper; my %hash; my @records; while () { chomp; my ($id, $first, $second) = split /\s+/; my $record = [ $id, $first, $second ]; # Build hashes indexed by first and second numbers push(@{$hash{first}{$first}}, $record); push(@{$hash{second}{$second}}, $record); # Build array of records push(@records, $record); } print "\%hash:\n"; print Dumper(\%hash); print "\n\n"; print "\@pairs:\n"; print Dumper(\@records); print "\n\n"; print "records with first number 6, from hash:\n"; foreach my $record (@{$hash{first}{6}}) { print "\t", join(',',@$record), "\n"; } print "records with first number 6, from array:\n"; foreach my $record (grep { $_->[1] == 6 } @records) { print "\t", join(',',@$record), "\n"; } __DATA__ 1 1 1 2 1 2 3 2 3 4 2 4 5 3 5 6 3 6 7 4 7 8 4 6 9 5 8 10 5 9 11 6 10 12 6 9