Page 1 of 1

Script help

Posted: 16 Nov 2019 02:37
by Lucy
Hey im trying to change variable value of service_state from the standard 0, 1, 2 and 3 to a text format. Lol ive tried a few different approaches but ultimately i have no idea what im doin... i do try though.
Here is my script. Please let me know how easy it is to fix it!!
(Keep in mind i am a total noob and the tutorials and script examples are not enough for me to understand script operations

SCRIPT:
global_phone_service_state = {service_state};
if(global_phone_service_state==0)
{
replace(global_phone_service_state, 0, "in service");
};
if(global_phone_service_state==1)
{
replace(global_phone_service_state, 1, "out of service");
};
if(global_phone_service_state==2)
{
replace(global_phone_service_state, 2, "emergencies only");
};
if(global_phone_service_state==3)
{
replace(global_phone_service_state, 3, "phone off");
};

Re: Script help

Posted: 16 Nov 2019 07:19
by Lucy
Ok... finaly after a lot of guessing and trials i finaly got a working script. It displayed the string instead of the number. However the script looks messy and unnecessarily larger than i am sure it needs to be...

UPDATED SCRIPT:

global_phone_service_state=service_state;
tempvalue=getValue(global_phone_service_state, service_state);
if(tempvalue==0)
{
global_phone_service_state="in service";
}
else
{
if(tempvalue==1)
{
global_phone_service_state="out of service";
}
else
{
if(tempvalue==2)
{
global_phone_service_state="emergencies only";
}
else
{
if(tempvalue==3)
{
global_phone_service_state="phone off";
}}}};

Re: Script help

Posted: 17 Nov 2019 10:09
by HumourMe
I'm not yet experienced with the language but I think you can use lists or map to simplify and reduce to a couple of lines

Code: Select all

states = newList("in service", "out of service","emergencies only","phone off");
//assumes service_state is always bounded by [ 0, 3 ] or a test needed here
global_phone_service_state =states[service_state] ;

Re: Script help

Posted: 19 Nov 2019 04:17
by Lucy
Ah... yeah thats when it gets a lil more complicated for me😂 I'll have a lil play around. Thank you

Re: Script help

Posted: 22 Nov 2019 18:37
by Desmanto
@Lucy : This is another map object usage here. I just answered other example usage : viewtopic.php?f=5&t=8378

Instead of using multiple if() to check for the same value, you can use map object to define the key and value pair. In this case, you simply get lucky to get the index in number, so you can use list object. But in other times, you might not be that lucky. So this is another way to do the same thing, but using map object instead.

Code: Select all

states = newMapFromValues(
0, "in service",
1, "out of service",
2, "emergencies only",
3, "phone off" );

global_phone_service_state = states[service_state];