I want to make sure there is NOTHING IN THE LIST before I run additional code. How would I construct this? Is there a way to work this into switch statement?
if(listname == null) //???
ID:155733
Apr 2 2011, 1:29 pm
|
|
I have a list with different strings in it (player names)
I want to make sure there is NOTHING IN THE LIST before I run additional code. How would I construct this? Is there a way to work this into switch statement? if(listname == null) //??? |
In response to OrangeWeapons
|
|
Don't quote me on this but I think it depends if you want to keep it as a list type or just as null.
If you do: listname=null then it is no longer a list and just a null var. If you want to keep the type you can do this: listname=list() Do you want to empty the list or just check that it is empty? A list could be empty and still have a length. It could be listname(null,null,null) which has a length of 3 but would typically be considered empty. So it kind of depends on how you manage your lists. |
In response to Asielen
|
|
Well i guess i need it to be completely empty. The goal is to make sure there is nothing in the list, not to MAKE it empty.
How would I go about removing some of the names in my list by their associated place? example: Removing listl[1]. del(listl[1]) //doesn't work Asielen wrote: Don't quote me on this but I think it depends if you want to keep it as a list type or just as null. > listname=null then it is no longer a list and just a null var. > listname=list() Do you want to empty the list or just check that it is empty? |
In response to Emasym
|
|
Emasym wrote:
if(!length(listname)) Woah. Thank you, and excuse me for being retarded but what exactly is this statement saying? If the length of listname is not the length of listname? |
In response to Lilcloudy1
|
|
Lilcloudy1 wrote:
Well i guess i need it to be completely empty. The goal is to make sure there is nothing in the list, not to MAKE it empty. > del(listl[1]) //doesn't work samplelist -= samplelist[1] |
In response to Lilcloudy1
|
|
It is checking to see if the length of the list is a non-zero value. Which may or may not work for you.
|
The /list type has a 'len' variable that tells you how many items are in the list, if it's 0 the list is empty.
if(!my_list.len)
|
In response to Nadrew
|
|
Nadrew wrote:
The /list type has a 'len' variable that tells you how many items are in the list, if it's 0 the list is empty. > if(!my_list.len) Though earlier mentioned through vague example, this method isn't very robust as it will produce a runtime error if the variable is null, or anything that doesn't have the 'len' property. |
In response to Lilcloudy1
|
|
Lilcloudy1 wrote:
Emasym wrote: length() is a built in proc that returns the length of a list, or 0 if it is not a list. That second condition allows other data types to be (accidently) put in, like null, and will not produce a runtime error if making an assumption that the data is a list. |
In response to Unknown Person
|
|
If it was null, or not of the type list. Depending on the context it could be a problem. It's better to have it throw a runtime error, thus forcing you to correct the problem, then to fix it with exception patchwork. Both solutions in the posts below hide errors. Nadrew's solution makes you confront, and fix errors, rather then having the code ignore garbage data.
Here is a *probably* bullet proof method of handling it. if(!my_list) my_list=list() This might seem like a lot of work, but almost all of this functionality can be encapsulated into procedures for easy use. |
In response to Hicup
|
|
It's actually pretty common for /list variables to be null without it being an error - many DM programmers get into the habit of nulling empty lists because you can only have 65535 of them globally - so, for example, if you wanted a /list on some type of turf, you'd definitely not want to have empty lists on them if there wasn't anything to go in there.
|
In response to Jp
|
|
Said limit doesn't exist outside of compile-time now (not saying you should leave unused list variables initialized). It's still better to have it give you an runtime error (or any kind of error, even if you make it yourself) if you're trying to improperly use the list. That way you can easily locate and debug where and why there was a failure.
|
if(listname[1]==null)
You could also do that with a for() loop if you keep track of the size of a list to check every spot in it.
You can make an entire list null I believe, don't quote me on it, but
listname = null