BookmarkSubscribeSubscribe to RSS Feed
twillkickers

Contributor

Joined:

May 21, 2018

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

Community Trekker

Joined:

May 1, 2017

Solution

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
2 REPLIES
cwillden

Community Trekker

Joined:

May 1, 2017

Solution

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
Highlighted
twillkickers

Contributor

Joined:

May 21, 2018

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

That worked right away! Thank you so much!