Description: We recently needed a utility to tell us what state or province a telephone area code came from. This took about five minutes (the hardest part was finding the data itself).
=head1 NAME

area_code.pl - What state/province is this area code from?

=head1 SYNOPSIS

perl area_code.pl [area_code]

=head1 AUTHOR

tim allen (zeno@timallen.org)

=cut

#!/usr/bin/perl
use strict;

# get the area code from the user
# User can either enter an area code on the command line or
# be prompted for it.
my $area_code;
if (!$ARGV[0]) {
    print "Input an area code: ";
    $area_code = <STDIN>;
} else {
    $area_code = $ARGV[0];
}
chomp($area_code);

# if the user enters nothing, exit.
if (!$area_code) {exit};

# loop through list of states/provinces
LISTING: while(<DATA>) {
    # store the state/province and list of area codes
    m/([^\d]+)(.*)\s?/;
    my $state=$1;
    my $numbers=$2;
    if ($numbers =~ m/$area_code/) {
            print "$area_code: $state\n";
            # once we have a match, don't continue
            # through the whole list
            last LISTING;
    }
}
__DATA__
Alabama 205 251 256 334 
Alaska 907 
Arizona 480 520 602 623 928 
Arkansas 501 870 
California 209 213 310 323 408 415 510 530 559 562 619 626 650 661 707
+ 714 760 805 818 831 858 909 916 925 949 
Colorado 303 719 720 970 
Connecticut 203 860 
Delaware 302 
District of Columbia 202 
Florida 305 321 352 386 407 561 727 786 813 850 863 904 941 954 
Georgia 229 404 478 678 706 770 912 
Hawaii 808 
Idaho 208 
Illinois 217 309 312 618 630 708 773 815 847 
Indiana 219 317 765 812 
Iowa 319 515 563 641 712 
Kansas 316 620 785 913 
Kentucky 270 502 859 606 
Lousiana 225 318 337 504 985 
Maine 207 
Maryland 240 301 410 443 
Massachusetts 339 351 413 508 617 774 781 857 978 
Michigan 231 248 313 517 616 734 810 906 989 
Minnesota 218 320 507 612 651 763 952 
Mississippi 228 601 662 
Missouri 314 417 573 636 660 816 
Montana 406 
Nebraska 308 402 
Nevada 702 775 
New Hampshire 603 
New Jersey 201 609 732 856 908 973 
New Mexico 505 
New York 212 315 347 516 518 607 631 646 716 718 845 914 917 
North Carolina252 336 704 828 910 919 980 
North Dakota701 
Ohio 216 234 330 419 440 513 614 740 937 
Oklahoma 405 580 918 
Oregon 503 541 971 
Pennsylvania 215 267 412 484 570 610 717 724 814 878 
Rhode Island 401 
South Carolina 803 843 864 
South Dakota 605 
Tennessee 423 615 731 865 901 931 
Texas 210 214 254 281 361 409 469 512 682 713 806 817 830 832 903 915 
+936 940 956 972 979 
Utah 435 801 
Vermont 802 
Virginia 434 540 571 703 757 804 
Washington 206 253 360 425 509 
West Virginia 304 
Wisconsin 262 414 608 715 920 
Wyoming 307 
Alberta 403 780 
British Columbia 250 604 
Manitoba 204 
Newfoundland 709 
New Brunswick 506 
Northwest Territories 867 
Nova Scotia 902 
Nunavut 867 
Ontario 289 416 519 613 647 705 807 905 
Prince Edward Island 902 
Quebec 418 450 514 819 
Saskatchewan 306 
Yukon Territory 867 
Guam 671 
Northern Mariana Islands 670 
Puerto Rico 787 939 
US Virgin Islands 340 
Anguilla 264 
Antigua & Barbuda 268 
Bahamas 242 
Barbados 246 
Bermuda 441 
British Virgin Islands 284 
Cayman Islands 345 
Dominica 767 
Dominican Republic 809 
Grenada & Carriacou 473 
Jamaica 876 
Montserrat 664 
Nevis & St. Kitts 869
St. Lucia 758 
St. Vincent & The Grenadines 784 
Trinidad & Tobago 868 
Turks & Caicos 649
American Samoa 684
Replies are listed 'Best First'.
Re: Telephone Area Code Finder
by lhoward (Vicar) on Nov 06, 2001 at 22:52 UTC
    One thing you may want to add to your program is the ability to load the data from NANPA (via LWP), so you can semi-automatically update your table as new area codes are added.
Re: Telephone Area Code Finder
by runrig (Abbot) on Nov 06, 2001 at 21:55 UTC
    Just a note...you seem to have most everything else, so I thought I'd mention you're missing American Samoa. (Why the heck would I notice or care?? My in-laws lived there for a time so I thought I'd just check your list for it). I found a list here: http://www.superphone.net/areacode.html. I don't know how it compares to your list, but it does have Am. Samoa :-)

    Update: Actually, according to what I see here, its a country code and not (yet) a NANPA area code (though that number IS reserved for the purpose), which explains why all our calls to AS were dialed and billed as international calls :-)

      Sorry Samoa! I went ahead and added it to the list. If anyone else notices something missing, please tell me. Thanks runrig!
Re: Telephone Area Code Finder
by CharlesClarkson (Curate) on Nov 12, 2001 at 13:24 UTC

    You could eliminate $number and add o to the area code match. Just check the whole line for the are code.

    LISTING: while(<DATA>) { next LISTING unless /$area_code/o; # store the state/province my ($state) = m/^(\D+)/; print "$area_code: $state\n"; # once we have a match, don't continue # through the whole list last LISTING; }



    HTH,
    Charles K. Clarkson

    It is not because things are difficult that we do not dare;
        it is because we do not dare that they are difficult.
    - Seneca the Elder