cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
anders_bankefor
Level III

If statement that checks for exsisting data table

Hi,

I'm trying to create a if statement that checks if a specific (table name is known) exists or not. If the table exists it should be appended to a journal. My code is as following:

//assigns the table to a variable.

Data Table( "Ber" ) << Subset( Invisible, (Selected Rows), Output Table Name( "Output" ) );

NC1 = Data Table( "Output" );

......

//Checks if the tables exists

If( try(Data Table ("Output"), TableExist = 0),

NC1 << journal

,

jrn << Append (Text Box ("Alla neg. prover har ett värde under 0.2"));

);

I've also tried with the following:

If( try(NC1, TableExist = 0),

NC1 << journal

,

jrn << Append (Text Box ("Alla neg. prover har ett värde under 0.2"));

);

Results:

Even though the table exists (no erros occurs while running the script) the table is not appended.

If the table does not exsist the text box is appended.

Why does this not work?

Kind regards

Anders

2 ACCEPTED SOLUTIONS

Accepted Solutions
David_Burnham
Super User (Alumni)

Re: If statement that checks for exsisting data table

This should help you:

//Checks if the tables exists

Try(

    Data Table("Output");

    tableExist = 1

,

    tableExist = 0

);

If( tableExist,

    show("exists")

,

    show("doesn't exist")

);

-Dave

View solution in original post

pmroz
Super User

Re: If statement that checks for exsisting data table

I use a function for this.  Once you've loaded the function, simply call it like this:

if (table_exists("Output"),

    // append to journal

    ,

    // don't append

);

Here's the function:

/*

Function Name: table_exists

Description: Returns 1 if the table exists, 0 if not

Arguments:

table_name  Table to check existence of

*/

table_exists = Function( {table_name},

      {Default Local},

   tbl_exists_flag = 0;

// 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;

);    // end table_exists function

View solution in original post

4 REPLIES 4
David_Burnham
Super User (Alumni)

Re: If statement that checks for exsisting data table

This should help you:

//Checks if the tables exists

Try(

    Data Table("Output");

    tableExist = 1

,

    tableExist = 0

);

If( tableExist,

    show("exists")

,

    show("doesn't exist")

);

-Dave
anders_bankefor
Level III

Re: If statement that checks for exsisting data table

works like a charm, thank you for your help.

May I ask why it doesnt work when I make a combined statement of If and try? It seems to me that JMP contucts the operations (no error is generated) but it didnt conduct all of the statement.

Kind Regards

Anders

David_Burnham
Super User (Alumni)

Re: If statement that checks for exsisting data table

The IF statement requires a condition that evaluates to true or false.  You are relying on the TRY function to do this, which is not its primary task, but it will return the value of the expression if evaluates.  But DATA TABLE("Output") gives a reference to a data table, it doesn't give a true/false answer.  In particular it doesn't give the answer true and therefore the IF expression never gets evaluated.

-Dave
pmroz
Super User

Re: If statement that checks for exsisting data table

I use a function for this.  Once you've loaded the function, simply call it like this:

if (table_exists("Output"),

    // append to journal

    ,

    // don't append

);

Here's the function:

/*

Function Name: table_exists

Description: Returns 1 if the table exists, 0 if not

Arguments:

table_name  Table to check existence of

*/

table_exists = Function( {table_name},

      {Default Local},

   tbl_exists_flag = 0;

// 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;

);    // end table_exists function