cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
AsymptoticCos
Level II

Extract Max number from a column and feed it to a for loop in the same jsl script

My JSL script has 3 parts. In the first part, I am creating a numeric column (Column A) with 100 rows filled with random integers between 50-1000.

In the 2nd part, I am extracting the max number in Column A and storing it to a variable i.

In the 3rd part, I am using i to create a for loop that creates i number of new columns in the same table. How can I do this? 

It seems I can store the data in i but the for loop is not running. 

 

8 REPLIES 8
jthi
Super User

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

Could you post what you have tried?

-Jarmo
AsymptoticCos
Level II

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

Here is the max value extraction from Column A. 

 

X = Eval(Col Max(:Name("Column A")));

 

Then when I use that as, for (i=1, i<=X ...

This for loop does not run. I checked that X is reporting the right number. 

jthi
Super User

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

You didn't post your code so difficult to say what is wrong. Here is working example

Names Default To Here(1);

dt = New Table("",
	Add Rows(100),
	New Column("ColumnA", Numeric, Continuous, Formula(Random Integer(50, 1000)))
);

max = Col Max(Column(dt, "ColumnA"));

For(i = 1, i <= max, i++,
	dt << New Column(Char(i), Numeric, Continuous, Set Each Value(i));
);
-Jarmo
AsymptoticCos
Level II

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

This code has the same problem as my code. When I run your code it does not create the columns that should have been created by the for loop. I am using JMP 14 so a bit older version. 

jthi
Super User

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

Are you getting error message? Are your columns being created but they have incorrect values? You said that for loop doesn't run but now it isn't creating columns (those are most likely separate issues). If you add Show(i); inside your loop does it print those values correctly to log?

Names Default To Here(1);

dt = New Table("",
	Add Rows(100),
	New Column("ColumnA", Numeric, Continuous, Formula(Random Integer(50, 1000)))
);

max = Col Max(Column(dt, "ColumnA"));

For(i = 1, i <= max, i++,
	Show(i);
);
-Jarmo
AsymptoticCos
Level II

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

When I run this, I only see /*; in the log. 

jthi
Super User

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

As I don't have access to JMP14 I have to just guess what could be different.

 

Maybe loop isn't run at all -> add debug print for max

Names Default To Here(1);

dt = New Table("",
	Add Rows(100),
	New Column("ColumnA", Numeric, Continuous, Formula(Random Integer(50, 1000)))
);

max = Col Max(Column(dt, "ColumnA"));
Show(max);

For(i = 1, i <= max, i++,
	Show(i);
);

other debug case, try loop without the table

Names Default To Here(1);
max = 10;

For(i = 1, i <= max, i++,
	Show(i);
);

If JMP14 did have Debugger (and you can get that working as that might not be that easy) you could also try that

jthi_0-1717003401520.png

 

-Jarmo
txnelson
Super User

Re: Extract Max number from a column and feed it to a for loop in the same jsl script

Here is a real simple example......

Names Default To Here( 1 );
dt = 
// Open Data Table: Blood Pressure.jmp
// → Data Table( "Blood Pressure" )
Open( "$SAMPLE_DATA/Blood Pressure.jmp" );

dt << New Column( "A", formula( Random Integer( 50, 1000 ) ) );

i = Col Max( dt:A );

For( k = 1, k <= i, k++,
	dt << New Column( "col" || Char( k ) )
);
Jim