Script help

Post your questions and help other users.

Moderator: Martin

Post Reply
Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Script help

Post by Lucy » 16 Nov 2019 02:37

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");
};

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Script help

Post by Lucy » 16 Nov 2019 07:19

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";
}}}};

HumourMe
Posts: 9
Joined: 09 Nov 2019 14:13

Re: Script help

Post by HumourMe » 17 Nov 2019 10:09

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] ;

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Script help

Post by Lucy » 19 Nov 2019 04:17

Ah... yeah thats when it gets a lil more complicated for me😂 I'll have a lil play around. Thank you

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Script help

Post by Desmanto » 22 Nov 2019 18:37

@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];
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply