cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar
miguello
Level VI

Difference between FOR and FOR EACH loops

Can 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?

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Difference between FOR and FOR EACH loops

My 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));
-Jarmo

View solution in original post

ErraticAttack
Level VI

Re: Difference between FOR and FOR EACH loops

It 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}

 

Jordan

View solution in original post

3 REPLIES 3
jthi
Super User

Re: Difference between FOR and FOR EACH loops

My 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));
-Jarmo
miguello
Level VI

Re: Difference between FOR and FOR EACH loops

Yes, 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?

ErraticAttack
Level VI

Re: Difference between FOR and FOR EACH loops

It 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}

 

Jordan