Detecting Empty Lists

Post your questions and help other users.

Moderator: Martin

Post Reply
98b427af

Detecting Empty Lists

Post by 98b427af » 26 Feb 2014 23:20

The isEmpty() function does not distinguish between a list with zero elements and a null variable. What's the best way to tell them apart?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Detecting Empty Lists

Post by Martin » 27 Feb 2014 16:26

You can check for null:
list == null
or check if list is not null:
list != null

98b427af

Re: Detecting Empty Lists

Post by 98b427af » 27 Feb 2014 16:55

Thanks!

98b427af

Re: Detecting Empty Lists

Post by 98b427af » 28 Feb 2014 22:05

A further note on this: isEmpty() returns true for null (undefined) variables, empty strings ("") and empty lists ([]), but *not* for empty maps ({}).

The isEmpty() behavior is thus inconsistent, and there is no way to determine if a map is defined but empty.

98b427af

Re: Detecting Empty Lists

Post by 98b427af » 28 Feb 2014 22:15

Also, even though you can treat a map as a string in many functions (substring, for example), the test

aMap == "{}"

returns false.

98b427af

Re: Detecting Empty Lists

Post by 98b427af » 28 Feb 2014 22:19

One more thing:

x = newMap();
matches(x, '\\{\\}')

returns true.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Detecting Empty Lists

Post by Martin » 01 Mar 2014 12:44

isEmpty does not exist for maps yet so map is converted to a string when the function is called. This is also the case for other method calls. I will add a new isEmpty function specifically for maps.

"{}" is not valid since it will try to evaluate the expression inside the braces and fails. A comparison newMap()=='{}' should work since the curly braces are not interpreted within single quotes.

Post Reply