cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
AT
AT
Level V

Setting a variable name within a for loop

Hi,

I like to set variable names such as name1, name2,... in a for loop with the following arrangement:

 

 

name1 = ntable;
name2 ="A"||"_"||Flow[1];
name3 ="A"||"_"||Flow[2];

 

 

and so on. 

I made a below script and got  some error:

 

name1 = ntable;
for (i=1, i<=Nitems, i++,
 Eval(
 Parse(
 "name"||char(i+1) = "A" ||"_"|| Flow[i];
 )
 )
);

 

I appreciate your help. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

Re: Setting a variable name within a for loop

What exactly are you struggling with? Are you receiving an error that you don't understand? I've shown the loops above that will work for you.

 

I suggested earlier that you use a list instead of trying to create indexed JSL variable names. I don't see anything in the code you posted here that suggests that a list won't work for you. I'd strongly encourage you to use lists instead.

 

flow = {"one", "two", "three", "four", "five"};

names = {};
files = {};

For( i = 1, i <= N Items( flow ), i++,
	names[i] = "A_" || flow( i );
	files[i] = Open( "$Documents/JMP/A_" || Flow[i] || ".jmp" );
	Create Excel Workbook( "$Documents/JMP/" || product_name || "_" || one_stage || ".xlsx", files, listexcelsheet );

);
-Jeff

View solution in original post

7 REPLIES 7
txnelson
Super User

Re: Setting a variable name within a for loop

You are not using the correct syntax for the renaming of a column.  Here is an example taken directly from

    Help==>Scripting Index==>Column Scripting

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
:Age << Set Name( "Time" );
Jim
Jeff_Perkinson
Community Manager Community Manager

Re: Setting a variable name within a for loop

@txnelson inteprets your question differently than I do.

 

I think you want to create a series of JSL variables with an index number as a part of the variable name.

 

You can do that but you'll need to build the entire expression as a string before you try to Parse() it.

 

Like this:

flow = {"two", "three", "four"};

name1 = "A_one";
For( i = 1, i <= N Items( flow ), i++,
	Eval( Parse( "name" || Char( i + 1 ) || "= \!"A_" || flow[i] || "\!";" ) )
);

However, I'm not sure why you'd want to do this instead of using a list or associative array which has the indexing built in.

names = {};
names[1] = "A_one";
For( i = 1, i <= N Items( flow ), i++,
	names[i + 1] = "A_" || flow[i]
);

Show( names[1], names[2], names[3], names[4] );

 

 

-Jeff
AT
AT
Level V

Re: Setting a variable name within a for loop

Thanks Jeff. Both works fine but I like first one since my code is based on that definition. Instead of name1, I also like to define

 

name1jmp = "$Documents/JMP/" || "A" || "_" || Flow[i] || ".jmp";

 

Can you help me how to add that to previous script? Thanks

 

Jeff_Perkinson
Community Manager Community Manager

Re: Setting a variable name within a for loop

The technique is exactly the same. You're building up a string that you're going to parse into an expression and evaluate it.

 

One thing that might be confusing you is the \!" below. Because you need your string to contain a " you need to escape it using \!.

 

flow = {"two", "three", "four"};

name1 = "A_one";
For( i = 1, i <= N Items( flow ), i++,
	expr_string = "name" || Char( i + 1 ) || "jmp = \!"$Documents/JMP/" || "A_" || Flow[i] || ".jmp\!"";
	Show( expr_string );
	Eval( Parse( expr_string ) );
);
-Jeff
AT
AT
Level V

Re: Setting a variable name within a for loop

Thank you very much Jeff. I need a bit more help after I got the first part. Here is the script that works fine below:

 

If( Nitems == 5,
name1 = ntable;
name2 = "A" || "_" || Flow[1];
name3 = "A" || "_" || Flow[2];
name4 = "A" || "_" || Flow[3];
name5 = "A" || "_" || Flow[4];
name6 = "A" || "_" || Flow[5];
 
name1jmp = "$Documents/JMP/" || "A" || "_" || Flow[1] || ".jmp";
name2jmp = "$Documents/JMP/" || "A" || "_" || Flow[2] || ".jmp";
name3jmp = "$Documents/JMP/" || "A" || "_" || Flow[3] || ".jmp";
name4jmp = "$Documents/JMP/" || "A" || "_" || Flow[4] || ".jmp";
name5jmp = "$Documents/JMP/" || "A" || "_" || Flow[5] || ".jmp";
 
 
file1 = Open( ntablejmp );
file2 = Open( name1jmp );
file3 = Open( name2jmp );
file4 = Open( name3jmp );
file5 = Open( name4jmp );
file6 = Open( name5jmp );
 
 
listx = Eval List( {name1, name2, name3, name4, name5, name6} );
 
 
listexcelsheet = Eval List( {name1, name2, name3, name4, name5, name6} );
 
Create Excel Workbook( "$Documents/JMP/" || product_name || "_" || one_stage || ".xlsx", listx, listexcelsheet );
 
);

 

I am trying to create a for loop so that it works for any Nitems. I am using MAC machine and that's why it has be done this way. Thanks for all your continuous suppprt.

Jeff_Perkinson
Community Manager Community Manager

Re: Setting a variable name within a for loop

What exactly are you struggling with? Are you receiving an error that you don't understand? I've shown the loops above that will work for you.

 

I suggested earlier that you use a list instead of trying to create indexed JSL variable names. I don't see anything in the code you posted here that suggests that a list won't work for you. I'd strongly encourage you to use lists instead.

 

flow = {"one", "two", "three", "four", "five"};

names = {};
files = {};

For( i = 1, i <= N Items( flow ), i++,
	names[i] = "A_" || flow( i );
	files[i] = Open( "$Documents/JMP/A_" || Flow[i] || ".jmp" );
	Create Excel Workbook( "$Documents/JMP/" || product_name || "_" || one_stage || ".xlsx", files, listexcelsheet );

);
-Jeff
AT
AT
Level V

Re: Setting a variable name within a for loop

Thanks Jeff. I tried your last script and changed the files and listexcelsheet to names and works perfectly. Thanks again for all your support.