'Check if a list is defined and avoid the "UNDEFINED" error

In the original TI-BASIC (for TI-83/84+) is there a way to check if a list has been defined?

Currently calling dim(⌊LIST) will return an error if the list is not defined.

enter image description here

Is there a way to handle that error gracefully?

The only hacky way I can think of doing so is to redefine the list with more items than you're expecting such as 99→dim(⌊LIST) and check if the first few values are not zero. But that seems wasteful and slow.

Any suggestions?



Solution 1:[1]

Unfortunately there doesn't seem to be a clean, simple function for checking if a list already exists, but thanks to harold who posted this as a comment, there is a workaround:

The SetUpEditor command.

This is typically used for specifying which lists are displayed in the list editor, but the command has the side-effect of creating a zero-length list if it does not exist yet.

So here's some sample code, with comments:

"Create two empty lists if they do not exist yet
SetUpEditor FOO,BAR

"Check the size of FOO
dim(?FOO)?X

"Clean up by returning the list editor back to
"its default state (?1-?6)
SetUpEditor 

Solution 2:[2]

If you have control during the lists's creation, then another workaround would be to use another list with only one element (or a list of another fixed size, or a letter variable) as a flag that indicates this main list exists.

Something like this, for lists LMAIN and LFLAG:

1->dim(LFLAG
If 5784923472?LFLAG(1
Then
"assume LMAIN does not exist because flag hasn't been set
"insert optional other code here
0->dim(LMAIN
5784923472->LFLAG(1
End

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon East
Solution 2 Quaris