Subscribe Bookmark RSS Feed

Check function arguments with isempty?

pmroz

Super User

Joined:

Jun 23, 2011

I have a function called table_exists.  You pass it a table name as a string and it returns true if the table exists, false if not.  I recently decided to update it by checking if the input argument was even bound to anything, but it doesn't like it.

For example:

isempty(xyz)

Returns a 1 in the log, meaning that xyz isn't bound to anything.  But if I try

table_exists(xyz)

I get this error message:

ERROR: Name Unresolved: xyz in access or evaluation of 'xyz' , xyz

Here's the function:

table_exists = Function( {table_name},

      {Default Local},

    tbl_exists_flag = 0;

    if (!isempty(table_name),

// Loop over all tables and see if this one exists

        num_tables = ntable();

        For (i = 1, i <= num_tables, i++,

            one_name = Data Table(i) << Get Name;

            if (one_name == table_name,

                tbl_exists_flag = 1;

                break();   // jump out of the loop now that we found the table

            );

        );

    );

    tbl_exists_flag;

);

3 REPLIES
michael_jmp

Staff

Joined:

Jun 23, 2011

I think the error message is there because you are passing xyz (an undefined JSL variable) to the function. I think JSL is trying to parse xyz (prior to running the code of your function) and it fails because it is empty. Try putting xyz in quotes in your function call:

table_exists("xyz")

Michael Crotty
Sr Statistical Writer
JMP Development
pmroz

Super User

Joined:

Jun 23, 2011

The table_exists function is general purpose, so I wanted to handle the situation if it was called with an empty variable gracefully.

melaniedrake

Staff

Joined:

May 30, 2014

The xyz variable has to be evaluated so its value can be passed into the function.


I can think of two ways to handle this. If you're using a given variable, you can assign it to Empty() at the start of the script. xyz = Empty(). If it later gets a name, then that will pass through; if it never gets a name, then it is empty.


The other way would be to wrap the function call in a try and handle the case of an undefined variable before you pass it to the function. try(table_exists(xyz), "no table name")