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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.