Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Validate Country by ISO 3166 data

by seattlejohn (Deacon)
on Mar 07, 2002 at 04:21 UTC ( [id://149934]=note: print w/replies, xml ) Need Help??


in reply to Validate Country by ISO 3166 data

One thought on initialization: To improve readability and perhaps simplify maintenance, I would be inclined to store the list of information in a __DATA__ section and create the hash dynamically, something like this:
my %country_hash; chomp (my @data_rows = <DATA>); foreach my $this_country (@data_rows) { my $country_code = substr($this_country,0,2); my $country_name = substr($this_country,3); $country_hash{$country_code} = $country_hash{$country_name} = $this_ +country; } # ... your tie code ... __DATA__ AF Afghanistan AL Albania DZ Algeria .....

In my eyes this would be easier to read and maintain. Also, special chars like the apostrophe Cote d'Ivoire no longer require escaping.

Replies are listed 'Best First'.
Re: Re: Validate Country by ISO 3166 data
by shotgunefx (Parson) on Mar 07, 2002 at 06:36 UTC
    Actually that's how I generated the hash. Pasted it in a temp script. I was wondering myself is it quicker for perl to parse it or to read it from data. Anyone have any thoughts?

    -Lee

    "To be civilized is to deny one's nature."
Re: Re: Validate Country by ISO 3166 data
by drewbie (Chaplain) on Mar 07, 2002 at 18:00 UTC
    A little cleaner looking w/o any extra vars:
    while (<DATA>) { chomp; $country_hash{substr($_,0,2)} = substr($_,3); }
Re: Re: Validate Country by ISO 3166 data
by krazken (Scribe) on Mar 08, 2002 at 15:57 UTC
    I totally agree. Making it more control file type driven makes it much easier to maintain and keeps you out of code that you know already works. Plus you can add more to the list and not have to worry about changing any code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://149934]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found