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
Stokes
Level IV

JSL multiple column comparison to a fix number using for loop, and generate a new column for output

Hi JSL Experts, I am a starter of JSL, I want to perform multiple column comparision, the column name is, Col1, Col2, Col3 etc. I am using a For loop for this comparison (see below for whole scripts), and if it falls into the formula (See below), it  will output 0 or 1. 1. What I know is, If I put :Col1 into the script :Col1, the script works fine as below. 2. What I don’t know is, If I replace :Col1 into Column( dt, i ), the script doesn’t work. I also tried use, :Name, :"Name", but none of it works to replace :Col1. Anyone know how to do the multiple column comparision? Example of the changed parts: This works fine: New Column(Name, Character, "Nominal", Formula(If( 1 > :Col1 >0, 0,1)) This doesn’t works: New Column(Name, Character, "Nominal", Formula(If( 1 > :Column( dt, i ) >0, 0,1)) Example of the whole scripts: dt1 = table1; dt = table2; For( i = 1, i < N Col( dt ) + 1, i++, Name = Column( dt, i ) << get name; If( Contains( Name, "ABCD" ), For( j = 1, j <= N Row( dt1 ), j++, GGG = Column( dt1, "GGGer" )[j]; New Column( Name, Character, "Nominal", Formula( If( 1 > :Col1 > 0, "0", "Fail" ) ) ); ); ) ); );
1 ACCEPTED SOLUTION

Accepted Solutions
uday_guntupalli
Level VIII

Re: JSL multiple column comparison to a fix number using for loop, and generate a new column for out

Hello @Stokes , 
         Welcome to the JSL Community. The community should be able to help you address your problem. However, for your future posts , may I suggest a small thing, when you post code , kindly insert using one of the following options shown below. This will help format the code and make it easier for community users to understand your code and help answer your questions.

image.png

Now that we have gotten that out of the way , lets see how to address your problem: 
If I understand your question correctly - 

1. How to compare columns in a data table against any parameter ? 
2. Why does a column reference ( Col = Column(dt,i); ) not directly replace a column name (:Col1) in a function call ? 

 

I will try to address these questions as best as I can - so please let me know if I dont address anything in my response .

 

Clear Globals(); Clear Log(); 

// Open Sample Data 
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" ); 

// Acquire Column Names 
ColNames = dt1 << Get Column Names("String"); 

// Define a searchword and look for it in column names 
DesiredColsPos1 = list();  
DesiredColsPos2 = List(); 
KeyWord1 = "age"; 

for(i = 1 , i <= N Cols(dt1), i++,
		If(Contains(ColNames[i],KeyWord1),
			Insert Into(DesiredColsPos1,i); 
		  ); 
   );
   
// Display Result
Show(DesiredColsPos1);

// Alternatively 
for(i = 1 , i <= N Cols(dt1), i++,
		Col = Column(dt1,i); // This gets a reference for the column 
		Show(Col);
		ColName = Col << Get Name; 
		If(Contains(ColName,KeyWord1),
			Insert Into(DesiredColsPos2,i);
		  ); 
   );
   
// Display Result
Show(DesiredColsPos2);



 

Best
Uday

View solution in original post

2 REPLIES 2
uday_guntupalli
Level VIII

Re: JSL multiple column comparison to a fix number using for loop, and generate a new column for out

Hello @Stokes , 
         Welcome to the JSL Community. The community should be able to help you address your problem. However, for your future posts , may I suggest a small thing, when you post code , kindly insert using one of the following options shown below. This will help format the code and make it easier for community users to understand your code and help answer your questions.

image.png

Now that we have gotten that out of the way , lets see how to address your problem: 
If I understand your question correctly - 

1. How to compare columns in a data table against any parameter ? 
2. Why does a column reference ( Col = Column(dt,i); ) not directly replace a column name (:Col1) in a function call ? 

 

I will try to address these questions as best as I can - so please let me know if I dont address anything in my response .

 

Clear Globals(); Clear Log(); 

// Open Sample Data 
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" ); 

// Acquire Column Names 
ColNames = dt1 << Get Column Names("String"); 

// Define a searchword and look for it in column names 
DesiredColsPos1 = list();  
DesiredColsPos2 = List(); 
KeyWord1 = "age"; 

for(i = 1 , i <= N Cols(dt1), i++,
		If(Contains(ColNames[i],KeyWord1),
			Insert Into(DesiredColsPos1,i); 
		  ); 
   );
   
// Display Result
Show(DesiredColsPos1);

// Alternatively 
for(i = 1 , i <= N Cols(dt1), i++,
		Col = Column(dt1,i); // This gets a reference for the column 
		Show(Col);
		ColName = Col << Get Name; 
		If(Contains(ColName,KeyWord1),
			Insert Into(DesiredColsPos2,i);
		  ); 
   );
   
// Display Result
Show(DesiredColsPos2);



 

Best
Uday
Stokes
Level IV

Re: JSL multiple column comparison to a fix number using for loop, and generate a new column for out

//add all pictures into a summary table

directory = Pick Directory();

fileNames = Files In Directory( directory );

summary = New Table( "summary", New Column( "name", character ), New Column( "rows", numeric ) );

For( iFile = 1, iFile <= N Items( fileNames ), iFile++,

filename = fileNames[iFile];

If( Ends With( filename, ".JPG" ),

// dt = Open( directory || filename );

summary << addrows( 1 );

summary:name = filename;

//summary:rows = N Rows( dt );

// Close( dt, "nosave" );

);

);