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.


In reply to Re: Which data structure should I use? by ig
in thread Which data structure should I use? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.