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
UserID16644
Level V

Check column name if existing

Hi,

I have a list of column names that I need to check from my table if it exist.

How will I check every columns of the table and prompt an error and stop the program from running if it is not existing?

 

col_list = {"Col1", "Col2", "Col3", "Col4", "Col5" };
//checks column if exist

//..code continue running
//if column doesn't exist
//..code will stop script & error prompt

  

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Check column name if existing

Here is a modification of the script that I previously posted.

I strongly suggest that you take the time to read the Scripting Guide found in the JMP Documentation Library,  under the Help pull down menu so you will be able to take this sample code and use it to solve your own scripting needs.

animals = Open( "$SAMPLE_DATA\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
notFound = {};
For( i = 1, i <= N Items( col_list ), i++,
	If( Try( Column( col_list[i] ) << get name, "" ) == "",
		Insert Into( notFound, col_list[i] )
	)
);

If( N Items( notFound ),
	nf = New Window( "Error:",
		V List Box(
			Text Box( "The following columns are required" ),
			Text Box( "but were not found in the data table" ),
			List Box( notFound )
		)
	);
	throw();
);
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Check column name if existing

The method I use is

col_list = {"col1", "col2", "col3"};
for(i=1,i<=N Items(col_list),i++,
    if(try(column(col_list[i])<<get name,"") == "", throw());
);
Jim
UserID16644
Level V

Re: Check column name if existing

animals = Open( "C:\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
For( i = 1, i <= N Items( col_list ), i++,
    If( Try( Column( col_list[i] ) << get name, "" ) == "",
        Throw()
    )
);



I'm sure I missed something, it's not working for me

txnelson
Super User

Re: Check column name if existing

What is it that isn't working?  I ran your code, and when it reached the column called "name" the code stopped.

The value of the variable "i" ended up as 5, since it stopped processing on the 5th loop.  If it had completed processing, the variable "i" would have been 6.

If there was code beyond this piece of code, it would not have been processed.

Jim
UserID16644
Level V

Re: Check column name if existing

The prompt where it will tell what column is missing
txnelson
Super User

Re: Check column name if existing

Here is a modification of the script that I previously posted.

I strongly suggest that you take the time to read the Scripting Guide found in the JMP Documentation Library,  under the Help pull down menu so you will be able to take this sample code and use it to solve your own scripting needs.

animals = Open( "$SAMPLE_DATA\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
notFound = {};
For( i = 1, i <= N Items( col_list ), i++,
	If( Try( Column( col_list[i] ) << get name, "" ) == "",
		Insert Into( notFound, col_list[i] )
	)
);

If( N Items( notFound ),
	nf = New Window( "Error:",
		V List Box(
			Text Box( "The following columns are required" ),
			Text Box( "but were not found in the data table" ),
			List Box( notFound )
		)
	);
	throw();
);
Jim