Simon E.
Simon E.

Reputation: 58460

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?

Possible Workaround:
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?

Upvotes: 1

Views: 336

Answers (2)

Quaris
Quaris

Reputation: 359

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

Upvotes: 1

Simon E.
Simon E.

Reputation: 58460

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 

Upvotes: 1

Related Questions