non existent array item

Post your questions and help other users.

Moderator: Martin

Post Reply
newturn
Posts: 30
Joined: 20 Apr 2016 08:36

non existent array item

Post by newturn » 04 Jan 2018 10:24

Hi there,

if i use

Code: Select all

notification_tag = antag[0]; // is not existent
i get the error: Value null is not a list or map.

That's fine. But there is no shorthand to test for an value. I always get an exception!!! And if you have many of those variables it's an unnecessary mess.


Do i have always to write something like this:

Code: Select all

if(isList(antag) and length(antag) >= 0)
{
   notification_tag = antag[0];
}
else
{
   notification_tag = "";
}
I would like to use something like:

Code: Select all

notification_tag = convertNull(antag[0], "");
or

Code: Select all

notification_tag = convertNull(!isList(antag[0]), "");
or better

just give a null or "" back

is this possible?

best regards
Screenshot_20180104-104434.png
Screenshot_20180104-104434.png (219.3 KiB) Viewed 12637 times
Screenshot_20180104-104442.png
Screenshot_20180104-104442.png (228.85 KiB) Viewed 12637 times

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

Re: non existent array item

Post by Desmanto » 04 Jan 2018 12:00

Automagic need a new function similar to excel, called isError(value) and ifError(value, value_if_error).
So far, there is no way to deal with the error/exception, except you catch it outside of the script, by changing the connector from normal to exception.
That will split your script into to parts, which is not convenient.

Meanwhile, you can set up your script like this

Code: Select all

if(length(antag)>0)
  notification_tag = antag[0];
notification_tag = convertNull(notification_tag, "");
Still not a perfect solution, but at least it doesn't need the branch else, which usually will takes up several lines for a tidy coding.

if we can have new function as ifError(), maybe it will be possible to use something like.

Code: Select all

notification_tag = ifError(antag[0], "");
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.

newturn
Posts: 30
Joined: 20 Apr 2016 08:36

Re: non existent array item

Post by newturn » 10 Jan 2018 00:51

Thank you very much. This is a nice way until we get an iferror function or something like that! I tried many ways but always got the is "null or not a list" error. I will try this also with empty maps.

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

Re: non existent array item

Post by Desmanto » 10 Apr 2018 17:21

Bumping old thread. I am indexing the thread, and reread this. I've found a better method to do it using inline expression. No need for isError() anymore.

This is the longer version of the script

Code: Select all

if(length(antag)>0)
{
   notification_tag = antag[0];
}
else
{
   notification_tag = "";
}
You can move the notification_tag to the front and assign the value immediately, putting it in single line :idea:

Code: Select all

notification_tag = if(length(antag)>0) antag[0] else "";
Ah, the single line code version is really satisfying. :D
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