Efficient way to change state abbrev to full name


#1

I’m looking for an efficient way to change state abbreviations to the full name.

I know it can be done with if statements or a case situation, but that would be huge memory usage.

Is there a function possibly that is existing or can it be added if not available?

Thanks

Gopack2


#2

I don’t understand the question fully… can you give an example?


#3

Sure robin.

I have a piston that is using google maps api to find a location via gps coordinates. When it finishes running it gives me a variable filled with information, including state

Here is the piston.


It is returning IL for Illinois, I would prefer the full name of the state, so that when I pass this to a speaker it says Illinois rather than what sounds like ill.


#4

Your best bet is the replace function:

https://wiki.webcore.co/Functions#replace


#5

replace(message, ‘IL’, ‘Illinois’)


#6

Ok, was trying to avoid a huge amount of those for each state.

Was hoping for a custom function.

Something like this:

Variable states = “AL”;”Alaska”, “AR”:”Arkansas “

Function({States},”AL”)

Would return Alaska


#7

You can do it in one long replace expression.

replace(message, ‘IL’, ‘Illinois’, ‘AL’, ‘Alaska’, ‘AR’, ‘Arkansas’)


#8

Hmm.
This looks interesting.
Would it be possible for you to post the code so that I can import this and have a play. If you don’t mind of course.


#9

What I’m trying to do I found this, just don’t know how to implement it in webcore


#10

That might work gets what I want done. More than one way to skin a cat


#11

Once you’ve typed the full expression with all states, please share so others don’t need to do the hard work as well :smile:


#12

@bobbles I posted the piston above. Use m4v3 to import it


#13

Just an extra thought, and I’m not 100% if this will be a problem or not… but it’s possible that the replace function might change words containing the abbreviations unintentionally, such as changing the word ‘are’ to ‘Arkansase’

Might be worth doing a few tests before you spend time typing out the whole lot!

If it does cause a problem, maybe replace the leading and trailing spaces as well:

replace(message, ‘ IL ’, ‘ Illinois ’, ‘ AL ’, ‘ Alaska ’, ‘ AR ’, ‘ Arkansas ’)


#14

One more approach, you can try a nested ternary… (short list below)

(sC == “AL” ? “ALABAMA” :
(sC == “AK” ? “ALASKA” :
(sC == “AZ” ? “ARIZONA” :
(sC == “AR” ? “ARKANSAS” :
(sC == “CA” ? “CALIFORNIA” :
(sC == “CO” ? “COLORADO” :
(sC == “WY” ? “WYOMING”)))))))


#15

What’s ‘sC’?

The message string contains more than just the abbreviated state, so I don’t see how a ternary operator could pick out the correct part to work on?


#16

this should return the long form of state:

define string shortState = 'AL,AK,AZ,...'
define string longState = 'Alabama,Alaska,Arizona,...'

arrayItem(indexOf(shortState, lookupState), longState)


#17

Oh, I was feeding it the state code == sC

And there’s @bangali’s method, Lists which I have not learned very well yet :slight_smile:


#18

before trying that one please check if indexof supports comma delimited string :slight_smile:

this does not actually use lists just comma delimited strings


#19

Thanks for the code.
The 2 global variables that you are using, what type are they?


#20

@bangali I’m getting an array out of bounds?