Page 1 of 1

non existent array item

Posted: 04 Jan 2018 10:24
by newturn
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 12764 times
Screenshot_20180104-104442.png
Screenshot_20180104-104442.png (228.85 KiB) Viewed 12764 times

Re: non existent array item

Posted: 04 Jan 2018 12:00
by Desmanto
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], "");

Re: non existent array item

Posted: 10 Jan 2018 00:51
by newturn
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.

Re: non existent array item

Posted: 10 Apr 2018 17:21
by Desmanto
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