in reply to Which data structure should I use?
Your comments suggest you want to be able to look up by either first or second number. For this, if you want to use hashes, you can build two hashes to support lookup by either number.
Alternatively, if you don't have too many records, you could simply populate an array with all the records and select from this array as necessary.
Both approaches are demonstrated in the following example:
use strict; use warnings; use Data::Dumper; my %hash; my @records; while (<DATA>) { 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
The hash will provide faster lookup if you have many records but the difference will be small if you have only a few records, as in your example.
|
|---|