cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
pmroz
Super User

Check function arguments with isempty?

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;

);

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Check function arguments with isempty?

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")

View solution in original post

3 REPLIES 3

Re: Check function arguments with isempty?

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

Re: Check function arguments with isempty?

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

Re: Check function arguments with isempty?

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")