Difference between FOR and FOR EACH loopsCan somebody explain why this:
dt = Open( "$SAMPLE_DATA/Fitness.jmp" );
col = { :Runtime, :RunPulse, :RstPulse };
y expr = Expr( Y() );
For( c = 1, c <= N Items( col ), c++,
Insert Into( y expr, col[c] );
);
Name Expr(y expr);produces expected output
Y( :Runtime, :RunPulse, :RstPulse )
but this:
dt = Open( "$SAMPLE_DATA/Fitness.jmp" );
col = { :Runtime, :RunPulse, :RstPulse };
y expr = Expr( Y() );
For each({coll, index},col,
Insert Into( y expr, coll );
);
Name Expr(y expr);produces this:
Y( ., ., . )
I would expect it to give the same result? How should I change FOR EACH loop to give the same result?
Consumer and Market ResearchSharing and Communicating ResultsRe: Difference between FOR and FOR EACH loopsIt is not a bug -- in this instance it is actually JMP being consistent. When indexing a list the result is returned unevaluated. When storing the result of an indexed item into a variable it then evaluates the result. Remember that For Each( {value, index}, list, expression... ) is equivalent to For( index = 1, index <= N Items( list ), index++, value = list[index]; expression... )
A = {1 + 2, 2 + 3, 3 + 4};
b = {};
res = A[1];
Insert Into( b, A[1] );
Insert Into( b, res );
b
/*:
{1 + 2, 3}
Re: Difference between FOR and FOR EACH loopsYes, this is it.
It is a strange behavior, since 1. we have For Each Row statement for by row evaluation, and 2. For Each should treat a list (and I'm trying to work with a list of references to columns) same as For loop.
Is this a bug or is it intended behavior?
Re: Difference between FOR and FOR EACH loopsMy guess is that the columns get evaluated into current rows values in For Each (you can test this by adding Row() = 1; for example). You might be able to avoid this by using Name Expr()
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Fitness.jmp");
col = {:Runtime, :RunPulse, :RstPulse};
y expr = Expr(Y());
For(c = 1, c <= N Items(col), c++,
Insert Into(y expr, col[c]);
);
Show(Name Expr(y expr));
Row() = 1;
y expr = Expr(Y());
For Each({coll, index}, col,
Insert Into(y expr, coll);
);
Show(Name Expr(y expr));
y expr = Expr(Y());
For Each({coll, index}, col,
Insert Into(y expr, Name Expr(coll));
);
Show(Name Expr(y expr));