cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
twillkickers
Level III

Variable names that change every time a loop is iterated

I am trying to create a new column formula that changes variable names every time a for loop is iterated. See below for the JSL code:

 

For(i = 1, i <= 5, i++,
dtMachineStartandStopTimes << New Column(:Machine||char(i)||RunDuration, Formula(:Machine||char(i)||StopTime-:Machine||char(i)||StartTime));
);

Each row in dtMachineStartandStopTimeshas columns labeled "Machine1StartTime" thru "Machine5StartTime" as well as "Machine1EndTime" through "Machine5EndTime". The code above does not work, but I feel like there should be a simple way to make it work.

 

FYI, I have already viewed the post at https://community.jmp.com/t5/Discussions/How-to-create-variable-names-depending-on-loop-number/td-p/..., but I cannot figure out what the poster is doing.

1 ACCEPTED SOLUTION

Accepted Solutions
cwillden
Super User (Alumni)

Re: Variable names that change every time a loop is iterated

The first argument of New Column() must be a string.  Try this "Machine"||char(i)||"RunDuration" instead.

 

If I was to interpret your formula the way JMP would, it be something like "take the value for the current row in the column "Machine" and append the current value of the index and append the value of the variable StopTime... etc.  You can't construct a column reference that way.  In this case, you are going to need parse the formula before you apply it to get rid of the "char(i)" stuff because that is going to break as soon as the value of "i" is no longer what was when the formula was created, or is undefined.  Here's how to do that:

For(i = 1, i <= 5, i++,
	Eval(Parse(Eval Insert("\[dtMachineStartandStopTimes << New Column("Machine^i^RunDuration", Formula(:Machine^i^StopTime - :Machine^i^StartTime)) ]\")));
);

 

-- Cameron Willden

View solution in original post

2 REPLIES 2
cwillden
Super User (Alumni)

Re: Variable names that change every time a loop is iterated

The first argument of New Column() must be a string.  Try this "Machine"||char(i)||"RunDuration" instead.

 

If I was to interpret your formula the way JMP would, it be something like "take the value for the current row in the column "Machine" and append the current value of the index and append the value of the variable StopTime... etc.  You can't construct a column reference that way.  In this case, you are going to need parse the formula before you apply it to get rid of the "char(i)" stuff because that is going to break as soon as the value of "i" is no longer what was when the formula was created, or is undefined.  Here's how to do that:

For(i = 1, i <= 5, i++,
	Eval(Parse(Eval Insert("\[dtMachineStartandStopTimes << New Column("Machine^i^RunDuration", Formula(:Machine^i^StopTime - :Machine^i^StartTime)) ]\")));
);

 

-- Cameron Willden
twillkickers
Level III

Re: Variable names that change every time a loop is iterated

That worked right away! Thank you so much!