• nobofy (unregistered) in reply to ME2

    What if they add more states?

  • Larry (unregistered) in reply to nobofy
    nobofy:
    What if they add more states?
    ($states) = @_ or 'AL AK AZ AR CA ...'; return index ($states, "$state ") / 3;

    Happy now? You heathens can pass in our own list of states, or accept the default from God's Country, or add states as needed.

  • Anonymous (unregistered)

    Weird. It's almost like there should be something called ENUMs for this exact use case...

  • (cs)

    Another weirdness: The abbreviations use the alphabetical order of the full name of the states. Thus AR comes after AZ, IA comes after IN, etc.

  • golddog (unregistered) in reply to cscastle
    cscastle:
    Another weirdness: The abbreviations use the alphabetical order of the full name of the states. Thus AR comes after AZ, IA comes after IN, etc.

    I wonder if the values are in some database somewhere, and this was an attempt to save db interactions. Originally, when someone had been building up the dbo.States table, they forgot Mississippi until the end, and that just is reflected here.

    Or, the states are ordered by the amount they spend on education or something where Mississippi finishes last.

  • (cs) in reply to Larry
    Larry:
    return index('ALAKAZARCA...', $state) / 2;

    Chicago, LINIAKS

  • Old fart (unregistered)

    Mississippi is at the bottom because they rank last in almost every category. In Alabama they have a saying "Thank God for Mississippi!"

  • (cs) in reply to Meme
    Meme:
    More over Why convert to an arbitrary index in the first place?
    I considered this myself, but I think it boils down to this: reasons. Could be as simple as knowing which section where to write a report, which server where to redirect a request or which chart column where to draw the bar. Which means it's perfectly sensible to return 51 as a default value.
  • Federico (unregistered) in reply to Larry

    Not good, get_state_index("LA") would return 0. A bunch of invalid strings would also return a non-error value.

  • lesle (unregistered)

    Baker Island is 81.

    http://www.itl.nist.gov/fipspubs/fip5-2.htm

    (FWIW, I was using FIPS codes by the late '70s.)

  • (cs) in reply to Larry
    Larry:
    return index('ALAKAZARCA...', $state) / 2;

    Doesn't work, as pointed out, because they run into each other so LA will be read as split between what you meant to be two tokens.

    However use a separator

    index( 'AL.AK.AZ.AR.CA....', %state ) / 3;
    
  • (cs) in reply to Anonymous
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).

    So "AR" would convert to the enum AR.

  • Ole (unregistered) in reply to Larry
    Larry:
    return index('ALAKAZARCA...', $state) / 2;

    Bad idea ... try 'LA' and you may see why ;-)

    regards Ole

    Hint: 'LA' should return 17 and not 1 (or 0?).

  • Rob (unregistered) in reply to Larry

    Except that it would return 0 for 'LA' since that is at index 1 in your string

  • Ozz (unregistered) in reply to Cbuttius
    Cbuttius:
    refoveo:
    Surely 51 represents the UK, the 51st State of America. plaga
    52nd. Canada is the 51st
    Not according to the New Model Army.
  • Chris Hayes (unregistered) in reply to Larry

    That doesn't necessarily work, depending on the letter occurrence... consider:

    return index('MILAIL', 'IL') / 2;

    Granted, they aren't in alphabetical order in my example, but this is just to illustrate the possible issue with this method. It would return 0 (1 / 2), not 2 (4 / 2), for Illinois.

  • Chris Hayes (unregistered) in reply to Anonymous
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Or, hell, x = ['AL', 'AK', 'AZ',...].index(state); if (x == -1) x = 51;

  • (cs) in reply to Ozz
    Ozz:
    Cbuttius:
    refoveo:
    Surely 51 represents the UK, the 51st State of America. plaga
    52nd. Canada is the 51st
    Not according to the New Model Army.

    Cromwell's one or the 80s rock band?

  • Harrow (unregistered) in reply to pjt33
    pjt33:
    Larry:
    return index('ALAKAZARCA...', $state) / 2;
    $state = 'LA';
    return index('AL~AK~AZ~AR~CA...', $state) / 3;

    -Harrow.

  • Zost (unregistered)

    I hasten to point out that even if they created an instance of the State class, they still couldn't call "get_state_index": it's a private method.

  • Peter (unregistered)

    As whacked out as this list is, it doesn't quite match the fubar found in several US federal agencies.

    Last time I worked with the DEA databases, they were still using NB for Nebraska (the rest of the US switched from NB to NE back in 1969).

    The Department of Interior also lists a number of tribal areas in addition to states, so the coal mining office uses things like "Navaho - New Mexico" and "Navaho - Arizona" to try to prevent the screw ups where the tribes were not getting their share of the royalties (read Hillerman's book "Sinister Pig" for some more explanations of the situation).

  • Fernando (unregistered) in reply to Larry

    With this solution, Lousiana's value is 0.5?

  • Ozz (unregistered) in reply to Cbuttius
    Cbuttius:
    Ozz:
    Not according to the New Model Army.
    Cromwell's one or the 80s rock band?
    Well, seeing as Cromwell died almost 120 years before the USA got its independence, my money is on the latter. (The US had no real need to fight anyway. If they'd just waited 100 years or so the British would probably have just given it to them, like they did with the rest of their empire.)
  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to Larry
    Larry:
    nobofy:
    What if they add more states?
    ($states) = @_ or 'AL AK AZ AR CA ...'; return index ($states, "$state ") / 3;

    Happy now? You heathens can pass in our own list of states, or accept the default from God's Country, or add states as needed.

    $state = "Z" $state = "L AK"

    You need blanks on both ends.

  • Alexandros Katechis (unregistered) in reply to Owain

    The great state of DC is actually a district (District of Columbia).

    As per Wikipedia (http://en.wikipedia.org/wiki/Washington,_D.C.), purveyor of all true information on the internet, "As permitted by the U.S. Constitution, the District is under the exclusive jurisdiction of the United States Congress and is therefore not a part of any U.S. state."

  • Jack (unregistered) in reply to cscastle
    cscastle:
    proofer:
    Everyone missed the obvious. MI (listed twice) is not the abbreviation for Mississippi. So, 51 isn't error, so - quickly - which state is ACTUALLY missing?

    The second "MI" is actually "MT" (Montana). I had to bump up the text size to see the difference.

    Who ever said MI was Mississippi, anyway? MI is Michigan. MS is Mississippi, like the article says.

  • (cs) in reply to Harrow
    Harrow:
    pjt33:
    Larry:
    return index('ALAKAZARCA...', $state) / 2;
    $state = 'LA';
    return index('AL~AK~AZ~AR~CA...', $state) / 3; -Harrow.
    Ok, putting aside the fact that we have figured out an excellent solution, I think the real wtf is the problem. Under what circumstances would you need to do this conversion UNLESS you are trying to store your state as a tinyint in the db, in which case there is a special place in dante's 4th circle of hell for you. What other purpose would you need to represent a state as a number (an not even an in order number once you get past where ms should be).
  • chubertdev (unregistered)

    public string StateProvince;

    If you explicitly need to reference a state or province in the UI, you're probably doing something wrong. At least that's the smell I get from this type of code.

    Dealing with massive amounts of data coming from various sources, I've had to deal with everything, and standardizing it is hard, but wouldn't require this type of code.

    The second scenario is a drop down/textbox combo with clean-ish input from a user, but that still wouldn't require hard-coding values in the UI code.

  • (cs) in reply to Cbuttius
    Cbuttius:
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).

    So "AR" would convert to the enum AR.

    If it's Java,

    enum State {AR, AZ, etc...};
    State pirateState = State.valueOf("AR");
    
  • (cs) in reply to @Deprecated
    @Deprecated:
    Cbuttius:
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).

    So "AR" would convert to the enum AR.

    If it's Java,

    enum State {AR, AZ, etc...};
    State pirateState = State.valueOf("AR");
    

    No wait, let me change that:

    enum FederatedStatesOfMicronesia {
     Yap, Chuuk, Pohnpei, Kosrae
    };
    FederatedStatesOfMicronesia wtf = FederatedStatesOfMicronesia.valueOf("Yap");
    
  • Terry (unregistered) in reply to PiisAWheeL
    PiisAWheeL:
    What other purpose would you need to represent a state as a number
    As a foreign key into the state table, of course.

    But it isn't a number, because you don't do math on it. It just happens to consist entirely of short strings from the set [0-9]. That just happen to be in consecutive numerical order.

  • Tonsil (unregistered) in reply to Ozz

    I'm glad I'm not the only one thinking this.

  • (cs) in reply to @Deprecated
    @Deprecated:
    Cbuttius:
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).

    So "AR" would convert to the enum AR.

    If it's Java,

    enum State {AR, AZ, etc...};
    State pirateState = State.valueOf("AR");
    

    It isn't, it's C#. You can tell because string is all lowercase.

  • Muzer (unregistered) in reply to powerlord
    powerlord:
    @Deprecated:
    Cbuttius:
    Anonymous:
    Weird. It's almost like there should be something called ENUMs for this exact use case...

    Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).

    So "AR" would convert to the enum AR.

    If it's Java,

    enum State {AR, AZ, etc...};
    State pirateState = State.valueOf("AR");
    

    It isn't, it's C#. You can tell because string is all lowercase.

    It could be C++... I can't see anything there that would be invalid in C++, and the use of underscores instead of camel case points towards a more Unixy programmer.

  • Ben Jammin (unregistered)

    I hope most of these people responding don't read program requirements like they read comments. It seems like 9 out of 9 comments are saying the same reason why Larry's funny solution doesn't work and the other half are proposing other broken solutions.

  • Larry (unregistered) in reply to ¯\(°_o)/¯ I DUNNO LOL
    ¯\(°_o)/¯ I DUNNO LOL:
    Larry:
    ($states) = @_ or 'AL AK AZ AR CA ...'; return index ($states, "$state ") / 3;
    $state = "Z" $state = "L AK"
    Hey guess what buddy? If you write bad code, it doesn't work. How is that my fault?
  • (cs) in reply to JiP
    JiP:
    Holland (The Netherlands for you, geeks...)
    Yay! All of us who live in one but not the other are geeks!
  • n_slash_a (unregistered) in reply to BLs
    BLs:
    Including Puerto Rico isn't all that strange... in the banking industry it's been a common theme in most of my jobs. If you want strange consider my first finance company that had two divisions, domestic and international. Not all that unusual unless you consider that Hawaii was part of the international division yet Canada was part of the domestic division.
    It it the same way in the aviation industry, domestic vs international are defined by needing to cross an ocean. Not sure how that applies to the finance industry though...
  • Planar (unregistered) in reply to TGV
    TGV:
    This solution is not (much) slower than looking it up in some table. It's only 51 comparisons, whereas an efficient searcher would do, say 7

    What? 2^6 is 64, so a binary search does it in 6 comparisons maximum, 5 average.

    But I'm surprised that nobody has noticed TRWTF: representing the states as numbers. If you're not going to do arithmetic on them, they are not numbers and you shouldn't use numbers to represent them. Shouldn't that be obvious to every TDWTF reader?

  • Jack (unregistered) in reply to Planar
    Planar:
    TGV:
    This solution is not (much) slower than looking it up in some table. It's only 51 comparisons, whereas an efficient searcher would do, say 7

    What? 2^6 is 64, so a binary search does it in 6 comparisons maximum, 5 average.

    But I'm surprised that nobody has noticed TRWTF: representing the states as numbers. If you're not going to do arithmetic on them, they are not numbers and you shouldn't use numbers to represent them. Shouldn't that be obvious to every TDWTF reader?

    It's returning an index, presumably to some array of data for each "state". Is that so wrong?

  • Larry++ (unregistered)

    return $A eq'AL'?$X++:$A eq'AK'?$X++:$A eq'AZ'?$X++:$A eq'AR'?$X++:$A eq'CA'?$X++ ...;

  • jay (unregistered) in reply to rmarquet
    rmarquet:
    Smaller WTF: Not using the the FIPS state codes. That list already has reserved numbers for all of the territories that could someday become states.

    That seems like a WTF all its own. Why do we have a set of standard two-letter state/territory abbreviations, AND ALSO a set of standard two-digit codes for the same list? Why do we need two primary keys for the same list?

    Also, I'd quibble with your reference to "all the territories that could someday become states". How do you know that new states or territories might not be added someday in the future, or that existing territories might not be split or combined in whatever politics goes on in becoming states? And where are Tranquility Base and Ceres Colony on the list of potential future states?

  • jay (unregistered) in reply to Cbuttius
    Cbuttius:
    This is still ugly but better. Not sure if the syntax is correct for this language but you get the point
    enum StateCodeEnum { AL, AK, AZ, AR, ..., StateNotFound };
    private StateCodeEnum get_state_index(string state) 
    {
       if( state.size() < 2 ) 
         return StateNotFound;
    
       switch (state[0])
       {
          case 'A':
            switch( state[1] )
            {
               case 'L': return AL;
               case 'K': return AK;
               case 'Z': return AZ;
               default: return StateNotFound;
           }
            break;
    
          case 'C':
             ...
     
          default:
             return StateNotFound;
       }
    }
    

    Ugly but constant time and uses an enum rather than magic numbers.

    I'd rather something like this in C++ but not sure how feasible it would be in other languages

    private:
      enum StateCodeEnum = { ... };
      const size_t dim = 'Z'+1-'A';
      StateCodeEnumTable[dim][dim] = { StateNotFound }; 
      StateCode StateCodeEnum get_state_index(string const& state)
      {
          // verify length is 2 and both characters are letters
          return StateCodeEnumTable[state[0]-'A'][state[1]-'A'];
      }
    
     </div></BLOCKQUOTE>
    

    Define "better". This would be faster, but probably only marginally so. And the code is much more difficult to read and therefore to maintain.

  • jay (unregistered) in reply to Planar
    Planar:
    TGV:
    This solution is not (much) slower than looking it up in some table. It's only 51 comparisons, whereas an efficient searcher would do, say 7

    What? 2^6 is 64, so a binary search does it in 6 comparisons maximum, 5 average.

    But I'm surprised that nobody has noticed TRWTF: representing the states as numbers. If you're not going to do arithmetic on them, they are not numbers and you shouldn't use numbers to represent them. Shouldn't that be obvious to every TDWTF reader?

    As the example doesn't show us where this number is used, it's hard to say. If, say, they're using this function to convert a state abbreviation into a number so they can use it to index into an array in which they are keeping data about each state, that could be perfectly reasonable.

  • Joe Shmobinsky (unregistered) in reply to jay
    jay:
    rmarquet:
    Smaller WTF: Not using the the FIPS state codes. That list already has reserved numbers for all of the territories that could someday become states.

    That seems like a WTF all its own. Why do we have a set of standard two-letter state/territory abbreviations, AND ALSO a set of standard two-digit codes for the same list? Why do we need two primary keys for the same list?

    Also, I'd quibble with your reference to "all the territories that could someday become states". How do you know that new states or territories might not be added someday in the future, or that existing territories might not be split or combined in whatever politics goes on in becoming states? And where are Tranquility Base and Ceres Colony on the list of potential future states?

    Thanks for bringing it up - I was thinking "Jefferson" or "Jackson" - whatever some folks are planning in the Pac NW.

    /captcha: dolor: pain

  • Hmm (unregistered) in reply to Larry

    So someone misspells Kansas as KA and your function returns 1.5 as the index?

  • jay (unregistered)

    I'm not too disturbed by the if/then return/else structure. As others have said, it's only 51 tests max and no overhead, probably not all that much slower than a hash lookup or a binary search. If I get really bored maybe I'll run a test both ways and get some empirical data.

    But 51 as the error value annoys the heck out of me. The presence of Puerto Rico indicates it's not strictly states. What happens if they decide they need, say, DC and Guam? Then we get 0 - 50 = valid, 51 = error, 52 - 53 = valid. Yuck. Or the programmer will decide that he should insert DC and Guam at 51 and 52 and make the error value now 53. And then he'll miss one call when he changes this, and mysteriously all values for DC will be treated as errors, but just in one function.

  • jMerliN (unregistered)

    This isn't any worse than the sites that hard-code a massive list of cities/states/countries into a <select> tag's options. I see that all the time. It makes me wonder if someone was paid per line, just like all these super-epic cascading ifs and massive switches do (granted, massive switches are sometimes the only option, sort of like running into those speed barrels or ramps because your brakes are out).

  • jay (unregistered)

    I generally object to hard-coding lists, and I would almost certainly have put this in a database. But as lists go, this one is pretty stable. I think the last time we added a new state was 1959. It's not like this is highly dynamic. If the US annexes Vancouver or Isfahan as the 51st state, I'm sure we can take the time to update the table, recompile, and redeploy.

  • The Dane (unregistered) in reply to refoveo
    refoveo :
    Surely 51 represents the UK, the 51st State of America.
    In that case, Sir, I have to load my long ships with norse warriors and go pillage your country until you let go of Mercia and Northumbria again. They've been ours for approximately the last 1200 years and we are NOT letting go without a fight.

    Get of my lawn you youngling state.

Leave a comment on “Pick-a-State”

Log In or post as a guest

Replying to comment #:

« Return to Article