Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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!!

In reply to Re: U.S. State Names by aardvark
in thread U.S. State Names by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-06-22 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.