If you were to store this in a relational database, you could just dump it in a table and build three indices on top of it. Well, we can do something similar in Perl (except that our indices are not at all suitable for range queries), using hashes for the indices.

Here's some example code (assuming the codes and countries are unique, but currencies aren't).

#!/usr/bin/perl -w use strict; use warnings 'all'; my (%code, %country, %currency); while (<DATA>) { my $info = [split]; $code {$info -> [0]} = $info; $country {$info -> [1]} = $info; push @{$currency {$info -> [2]}} => $info; } sub info_by_code { my $code = shift; $code {$code} } sub info_by_country { my $country = shift; $country {$country} } sub info_by_currency { my $currency = shift; @{$currency {$currency}} } print "Code for Germany is ", info_by_country ("GER") -> [0], "\n"; print "People pay in Euros in ", join (", " => map {$_ -> [1]} info_by_currency ("EUR")), "\n"; __END__ US USA USD CA CAN CND DE GER EUR IT ITA EUR FR FRA EUR NL NET EUR BE BEL EUR CH SWI SFR $ ./countries Code for Germany is DE People pay in Euros in GER, ITA, FRA, NET, BEL $

Abigail


In reply to Re: data structure problem by Abigail-II
in thread data structure problem 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.