http://www.perlmonks.org?node_id=60817


in reply to U.S. State Names

An alternate way to get the United States' state names is by using the CPAN module Locale::SubCountry:

#!/usr/bin/perl -w use strict; use Locale::SubCountry; use Data::Dumper qw(Dumper); #Create an object representing a certain country my $subcountry = Locale::SubCountry->new('US'); #Query to find this country's subcountries. my %states = $subcountry->code_full_name_hash; #Prints the name/code pairs for every US state print Dumper(\%states); __END__

Besides the obvious benefit of keeping these details in a central location, this module will tell you the state/province/county/etc of all the countries in the ISO 3166-2 sub country code list.

Updated: I changed the code example to use the method code_full_name_hash which returns a hash where the state code is the key and the value is the state's full name. This is so my example returns the same as tye's first example.

The method name was self describing, if you'd like the reverse (like I had before), use the full_name_code method.

Replies are listed 'Best First'.
Re: (dkubb) Re: (2) U.S. State Names
by bladx (Chaplain) on Feb 26, 2001 at 10:34 UTC
    Awesome point dkubb! I would never have thought of using that module to do that same thing tye was doing earlier on his own. This saves us a ton of time! Thanks for bringing this module up and letting us know about it! :-)

    bladx ~ ímucho veces yo tengo preguntas!