cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-311971%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ECheck%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-311971%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20have%20a%20list%20of%20column%20names%20that%20I%20need%20to%20check%20from%20my%20table%20if%20it%20exist.%3C%2FP%3E%3CP%3EHow%20will%20I%20check%20every%20columns%20of%20the%20table%20and%20prompt%20an%20error%20and%20stop%20the%20program%20from%20running%20if%20it%20is%20not%20existing%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ecol_list%20%3D%20%7B%22Col1%22%2C%20%22Col2%22%2C%20%22Col3%22%2C%20%22Col4%22%2C%20%22Col5%22%20%7D%3B%0A%2F%2Fchecks%20column%20if%20exist%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%20%20%20%2F%2F..code%20continue%20running%3CBR%20%2F%3E%0A%2F%2Fif%20column%20doesn't%20exist%3CBR%20%2F%3E%0A%20%20%20%20%2F%2F..code%20will%20stop%20script%20%26amp%3B%20error%20prompt%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-311971%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3EMass%20Customization%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312178%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Check%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312178%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHere%20is%20a%20modification%20of%20the%20script%20that%20I%20previously%20posted.%3C%2FP%3E%0A%3CP%3EI%20strongly%20suggest%20that%20you%20take%20the%20time%20to%20read%20the%20Scripting%20Guide%20found%20in%20the%20JMP%20Documentation%20Library%2C%26nbsp%3B%20under%20the%20Help%20pull%20down%20menu%20so%20you%20will%20be%20able%20to%20take%20this%20sample%20code%20and%20use%20it%20to%20solve%20your%20own%20scripting%20needs.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Eanimals%20%3D%20Open(%20%22%24SAMPLE_DATA%5CAnimals.jmp%22%20)%3B%0Adt_animals%20%3D%20Data%20Table(%20%22Animals%22%20)%3B%0A%0Acol_list%20%3D%20%7B%22species%22%2C%20%22subject%22%2C%20%22miles%22%2C%20%22season%22%2C%20%22name%22%7D%3B%0AnotFound%20%3D%20%7B%7D%3B%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(%20col_list%20)%2C%20i%2B%2B%2C%0A%20If(%20Try(%20Column(%20col_list%5Bi%5D%20)%20%26lt%3B%26lt%3B%20get%20name%2C%20%22%22%20)%20%3D%3D%20%22%22%2C%0A%20%20Insert%20Into(%20notFound%2C%20col_list%5Bi%5D%20)%0A%20)%0A)%3B%0A%0AIf(%20N%20Items(%20notFound%20)%2C%0A%20nf%20%3D%20New%20Window(%20%22Error%3A%22%2C%0A%20%20V%20List%20Box(%0A%20%20%20Text%20Box(%20%22The%20following%20columns%20are%20required%22%20)%2C%0A%20%20%20Text%20Box(%20%22but%20were%20not%20found%20in%20the%20data%20table%22%20)%2C%0A%20%20%20List%20Box(%20notFound%20)%0A%20%20)%0A%20)%3B%0A%20throw()%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312015%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Check%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312015%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EThe%20prompt%20where%20it%20will%20tell%20what%20column%20is%20missing%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312014%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Check%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312014%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EWhat%20is%20it%20that%20isn't%20working%3F%26nbsp%3B%20I%20ran%20your%20code%2C%20and%20when%20it%20reached%20the%20column%20called%20%22name%22%20the%20code%20stopped.%3C%2FP%3E%0A%3CP%3EThe%20value%20of%20the%20variable%20%22i%22%20ended%20up%20as%205%2C%20since%20it%20stopped%20processing%20on%20the%205th%20loop.%26nbsp%3B%20If%20it%20had%20completed%20processing%2C%20the%20variable%20%22i%22%20would%20have%20been%206.%3C%2FP%3E%0A%3CP%3EIf%20there%20was%20code%20beyond%20this%20piece%20of%20code%2C%20it%20would%20not%20have%20been%20processed.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312013%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Check%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312013%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Eanimals%20%3D%20Open(%20%22C%3A%5CAnimals.jmp%22%20)%3B%0Adt_animals%20%3D%20Data%20Table(%20%22Animals%22%20)%3B%0A%0Acol_list%20%3D%20%7B%22species%22%2C%20%22subject%22%2C%20%22miles%22%2C%20%22season%22%2C%20%22name%22%7D%3B%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(%20col_list%20)%2C%20i%2B%2B%2C%0A%20%20%20%20If(%20Try(%20Column(%20col_list%5Bi%5D%20)%20%26lt%3B%26lt%3B%20get%20name%2C%20%22%22%20)%20%3D%3D%20%22%22%2C%0A%20%20%20%20%20%20%20%20Throw()%0A%20%20%20%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EI'm%20sure%20I%20missed%20something%2C%20it's%20not%20working%20for%20me%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312011%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Check%20column%20name%20if%20existing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312011%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThe%20method%20I%20use%20is%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ecol_list%20%3D%20%7B%22col1%22%2C%20%22col2%22%2C%20%22col3%22%7D%3B%0Afor(i%3D1%2Ci%26lt%3B%3DN%20Items(col_list)%2Ci%2B%2B%2C%0A%20%20%20%20if(try(column(col_list%5Bi%5D)%26lt%3B%3CGET%20name%3D%22%22%3E%3C%2FGET%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
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