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%3E%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-311971%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHallo%2C%3C%2FP%3E%3CP%3EIch%20habe%20eine%20Liste%20mit%20Spaltennamen%2C%20die%20ich%20in%20meiner%20Tabelle%20%C3%BCberpr%C3%BCfen%20muss%2C%20falls%20sie%20vorhanden%20sind.%3C%2FP%3E%3CP%3EWie%20%C3%BCberpr%C3%BCfe%20ich%20alle%20Spalten%20der%20Tabelle%2C%20melde%20einen%20Fehler%20und%20stoppe%20die%20Ausf%C3%BChrung%20des%20Programms%2C%20wenn%20es%20nicht%20vorhanden%20ist%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%3EMassenanpassung%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%3EBetreff%3A%20%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312178%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHier%20ist%20eine%20Modifikation%20des%20Skripts%2C%20das%20ich%20zuvor%20gepostet%20habe.%3C%2FP%3E%0A%3CP%3EIch%20empfehle%20Ihnen%20dringend%2C%20sich%20die%20Zeit%20zu%20nehmen%2C%20den%20Skriptleitfaden%20zu%20lesen%2C%20der%20in%20der%20JMP-Dokumentationsbibliothek%20im%20Pulldown-Men%C3%BC%20%E2%80%9EHilfe%E2%80%9C%20zu%20finden%20ist%2C%20damit%20Sie%20diesen%20Beispielcode%20nehmen%20und%20ihn%20zur%20L%C3%B6sung%20Ihrer%20eigenen%20Skriptanforderungen%20verwenden%20k%C3%B6nnen.%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%3EBetreff%3A%20%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312015%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EDie%20Eingabeaufforderung%20zeigt%20an%2C%20welche%20Spalte%20fehlt%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312014%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312014%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EWas%20funktioniert%20nicht%3FIch%20habe%20Ihren%20Code%20ausgef%C3%BChrt%20und%20als%20er%20die%20Spalte%20%E2%80%9EName%E2%80%9C%20erreichte%2C%20stoppte%20der%20Code.%3C%2FP%3E%0A%3CP%3EDer%20Wert%20der%20Variablen%20%E2%80%9Ei%E2%80%9C%20betrug%20am%20Ende%205%2C%20da%20die%20Verarbeitung%20in%20der%205.%20Schleife%20gestoppt%20wurde.Wenn%20die%20Verarbeitung%20abgeschlossen%20w%C3%A4re%2C%20w%C3%A4re%20die%20Variable%20%E2%80%9Ei%E2%80%9C%206%20gewesen.%3C%2FP%3E%0A%3CP%3EWenn%20%C3%BCber%20diesen%20Code%20hinaus%20Code%20vorhanden%20w%C3%A4re%2C%20w%C3%A4re%20dieser%20nicht%20verarbeitet%20worden.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312013%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%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%3EIch%20bin%20mir%20sicher%2C%20dass%20ich%20etwas%20%C3%BCbersehen%20habe%2C%20es%20funktioniert%20bei%20mir%20nicht%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-312011%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20%C3%9Cberpr%C3%BCfen%20Sie%20den%20Spaltennamen%2C%20falls%20vorhanden%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-312011%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDie%20Methode%2C%20die%20ich%20verwende%2C%20ist%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