Subscribe Bookmark RSS Feed

creation table in loop for

KinKame

Community Trekker

Joined:

Nov 30, 2015

no idea why following is not working.

Sure im doing something wrong but could someone help me to understand?

If I try out of the loop for it works actually .... :-(

for(i=1, i=2, i++,

       td << select where (:Design == type_list & :Car_type == "BMW");

       td << Subset(

            Output Table( "list-" || char(type_list) ),

            Selected Rows( 1 ),

            Selected columns only( 0 )

       );

       td << clear select ();

);

1 ACCEPTED SOLUTION

Accepted Solutions
ms

Super User

Joined:

Jun 23, 2011

Solution

Second argument: try with i <= 2 instead of i = 2.

6 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Solution

Second argument: try with i <= 2 instead of i = 2.

KinKame

Community Trekker

Joined:

Nov 30, 2015

Ok I don't really understand why but its works. thank you! Very helpful

Eric_Hill

Staff

Joined:

Oct 1, 2013

For the "I don't really understand why" part:

The second argument to the for loop statement is an expression that is evaluated before each loop iteration.  As long as that expression returns true (any non-zero value), the loop iteration will proceed; as soon as that expression returns false, the loop will stop.

The expression "i = 2", which you had as the second argument in your original code, is an assignment statement, assigning the value 2 to the variable i.  That assignment statement will always return 2, which is treated as true, which means your loop is infinite.  If you wanted your loop to continue iterating as long as the variable i had the value 2, you would use:

i == 2

rather than "i = 2".  "i == 2" returns true if i is equal to 2 and false otherwise.  If you had used "i == 2" instead of "i = 2", however, your loop would not have run at all, because you just initialized i to the value 1 in the first argument ("i = 1"), so the expression "i == 2" will return false.

Using "i <= 2" as the second argument says "continue iterating this loop as long as the variable i is less than or equal to 2", causing the loop to iterate 2 times, once with i set to 1 and once with i set to 2.

Hope that helps,

Eric

nacka

Community Trekker

Joined:

Aug 4, 2016

MS's answer might be the one you were actually looking for, but I did have another way I handled tables in loops:  add them to a list.

Example:

Designlist = {};

for (i = 1, i <= 2, i++,

     td << select where (:Design == type_list & :Car_type == "BMW");

     Designlist =td << Subset(

            Output Table( "list-" || char(type_list) ),

            Selected Rows( 1 ),

            Selected columns only( 0 )

       );

);

This allows you to do further manipulations on the listed tables without having to reference them by name, they are now stored in a convenient variable.

You might also want to try to use your "type_list" itself as the max by replacing the "2" with "nitems(type_list)".

Edit: added the rest of the "Select where" code... don't know why that didn't copy in the first place.

KinKame

Community Trekker

Joined:

Nov 30, 2015

nice solution.Trying now. This might simplify many script which where becoming super long due to table creation.

yes for the nitems ... definitively but here was just as a try

KinKame

Community Trekker

Joined:

Nov 30, 2015

works perfectly after some tests ... thank you very much