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.