BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
abdulj

Community Trekker

Joined:

Jul 12, 2011

How to create variable names depending on loop number

Hello

I am trying to create variables dynamically depending on the number of times my loop runs. How can I go about doing this?

Example

for(i=1, i<x, i++,

    

     //some code goes here

    

     var || i = current data table();   //This obviously does not work, but I hope you get the idea. I would eventually like to have var1, var2, var3, etc... till the loop is done

    

     )

Does this make sense?

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
michaelhaslam_p

Community Trekker

Joined:

Sep 15, 2013

Solution

Re: How to create variable names depending on loop number

Abdulj,

 

Here is one way using Eval ( Substitute ( ))

 

// dv = dummy variable
x = 10;
For( i = 1, i <= x, i++,
	Eval(
		Substitute(
				Expr(
					dv_variable = i;
					Show( dv_variable );
				),
			Expr( dv_variable ), Parse( "Variable Name " || Char( i ) )
		)
	)
);

 

Michael

8 REPLIES 8
michaelhaslam_p

Community Trekker

Joined:

Sep 15, 2013

Solution

Re: How to create variable names depending on loop number

Abdulj,

 

Here is one way using Eval ( Substitute ( ))

 

// dv = dummy variable
x = 10;
For( i = 1, i <= x, i++,
	Eval(
		Substitute(
				Expr(
					dv_variable = i;
					Show( dv_variable );
				),
			Expr( dv_variable ), Parse( "Variable Name " || Char( i ) )
		)
	)
);

 

Michael

Byron_JMP

Staff

Joined:

Apr 26, 2012

Re: How to create variable names depending on loop number

Wasn't there a talk on this at the Advanced Scripting Panel discussion at the JMP Discovery conference Last week? It was a great seminar, I wonder if the author posted any notes on his talk.

JMP Systems Engineer, Pharm and BioPharm Sciences
David_Burnham

Super User

Joined:

Jul 13, 2011

Re: How to create variable names depending on loop number

Here is an alternate notation:

For( i = 1, i <= x, i++,
	Eval( Parse( Eval Insert( "\[
        var^i^ = Current Data Table()
    ]\" ) ) )
) 

-Dave

-Dave
mewing

Community Trekker

Joined:

Jun 23, 2011

Re: How to create variable names depending on loop number

Dave,

I've never seen that syntax before, do the ^ surround any variable to include in text?  I would have written:

tmp = "var" || char(i) || " = Current Data Table()";

eval(parse(tmp))

This is obviously less efficient than what you've written.

David_Burnham

Super User

Joined:

Jul 13, 2011

Re: How to create variable names depending on loop number

Yes enclose each variable with ^ ... ^ and have the jsl statement enclosed in the Eval( Parse( Eval Insert("\[ ...  ]\"))) pattern.

For example, the following code sets spec limits for a column:

Open("$SAMPLE_DATA/Big Class.jmp");

cHeight = Column("height");

Column("height") << Set Property( "Spec Limits", {LSL(55), USL(75), Target(65)} );

Let's say we have the following variable assignments that we would like to use:

colName = "height";

lower = 55;

upper = 75;

target = 40;

Then we can write:

Eval( Parse( Eval Insert("\[

  Column("^colName^") << Set Property( "Spec Limits", {LSL(^lower^), USL(^upper^), Target(^target^)} );

]\")))

-Dave

-Dave
abdulj

Community Trekker

Joined:

Jul 12, 2011

Re: How to create variable names depending on loop number

thanks guys!

KinKame

Community Trekker

Joined:

Nov 30, 2015

Re: How to create variable names depending on loop number

oh nice .... thank you very much. Another thing I need to learn in JMP ...

Highlighted
markbailey

Staff

Joined:

Jun 23, 2011

Re: How to create variable names depending on loop number

Why create a new variable for each reference? Why not store the data table reference in a list for access by index or an associative array for access by key?

Learn it once, use it forever!