Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: U.S. State Names

by aardvark (Pilgrim)
on Feb 26, 2001 at 06:31 UTC ( #60808=note: print w/ replies, xml ) Need Help??


in reply to U.S. State Names

Thanks for a great snippet tye!!
I must admit that it took me a while to get my brain around merlyn's regex. But after breaking it down it made perfect sense. unixwizard point out that the code wouldn't work on the 'other' US states, but I think that can be modified by changing the (?:\s\w+)? bit to (?:\s\w+)*
Here is merlyn's regex broken down, with comments:

\G # when progressively matching a string # with the 'g' flag # you can use the \G anchor to 'hold' the postition # just after the previous match # helps regex remember where it left off # allows you to go through a list efficiently # without using split or looping # Mastering Regular Expessions (p 236 - 240) \s* # matches zero or more spaces that # may come before before a name-value pair (\w\w) # match two word characters (alphanumeric plus '_') # parentheses assign matched letters to $1 # this is the state abbreviation \s+ # match one or more spaces between name-value pair (\w+ # match one or more word characters (?: # ?: allows for cluster-only parentheses, # no capturing and doesn't assign to $3 \s\w+ # match one space then one or many word characters )? # match zero or one of these clusters # allows match of state names with mulitple words # ie New York, West Viginia # does not match States with three words, # like 'Northern Mariana Island' # change trailing ? to * to match those '(?:\s\w+)*' ) # assigns state name to $2 /gx; # end of regex # g flag for global search # x flag to allow whitespace in regex # might also want to use c flag # c flag causes the match position to be retained # following an unsuccesful match # see: Effective Perl Programming (p.63) # # the complete regex looks like this: # # /\G\s*(\w\w)\s+(\w+(?:\s\w+)?)/g; #
Here is the Benchmark of the three routines
Benchmark: timing 1000 iterations of merlyn, tye_1, tye_2... merlyn: 0 secs ( 0.54 usr + 0.00 sys = 0.54 CPU) @ 1851.85/s (n=1000) tye_1: 1 secs ( 0.65 usr + 0.00 sys = 0.65 CPU) @ 1538.46/s (n=1000) tye_2: 2 secs ( 1.58 usr + 0.00 sys = 1.58 CPU) @ 632.91/s (n=1000)
Also here is a link to FIPS and ISO 3166 country codes in case anybody wants to apply this snippet to countries.
http://www.cia.gov/cia/publications/factbook/docs/app-f.html

Get Strong Together!!


Comment on Re: U.S. State Names
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-08-23 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls