ID:269817
 
Is there a way to check if a list is empty, preferably usable in an if statement?
Use the len property.

if(list.len) dothis() //If the list has a length greater than zero
else if(!list.len) dothat() //If the list has a length of zero
In response to Mobius Evalon
Mobius Evalon wrote:
Use the len property.

if(list.len) dothis() //If the list has a length greater than zero
> else if(!list.len) dothat() //If the list has a length of zero


Of course, the "if(!list.len)" part of that is pretty redundant since !list.len must be true if list.len is not =P.
In response to Wizkidd0123
I always use else as an error catcher, E.G.:
if(list.len) dothis()
else if(!list.len) dothat()
else world << "That can't be good."
In response to Mobius Evalon
Mobius Evalon wrote:
I always use else as an error catcher, E.G.:
if(list.len) dothis()
> else if(!list.len) dothat()
> else world << "That can't be good."


While sometimes it's good to catch cases that should never happen, there's a difference between what can't happen and what shouldn't. If list.len is nonzero, then the if() will catch it. Otherwise the list is empty, period. There is absolutely no way both ifs could fail. The extra if() on the else is useless here.

Error catching is a good idea and I recommend it where it's reasonable to use it. What you're doing here is not error catching, though; it's just a foolish waste of time.

Lummox JR