Cancel
Showing results for
Did you mean:

# Loop problem

Pioneer

Hi

I want create loop which will split one row to a few.

I want create something that:
num_parts = N
--------------------------------------
row1 = max Field*n1 > 2

row2 = max Field*n2 > 2

...

row(n) = Field *(N-n)

-------------------------------------
n=n1+n2+ ... - number of parts

max = max number of parts witch to qualify

So e.g. if Field one part = 0,45 and num parts = 9:
row1 = 0,45*4 = 1,8 > 2

row2 = 0,45*4 = 1,8 > 2

row 3 = 0,45*1 = 0,45  > 2

Any help what I can do that? How create that loop?

Regards

4 REPLIES 4

# Betreff: Loop problem

Gears Phenom

Hi Lucas,

I don't really understand what you are trying to do, I think your "formulas" don't really add up or seem inconsistent to me.

Could you please show us how the 2nd table should look like for your example (with part_1 num_parts = 5 and length=width=0.8) after your desired loop has run through?

Alex

___________________________________________________________
Alex Dilg, Consultant at SimPlan AG (www.SimPlan.de)
This post was good and/or helpful to you? Please provide some Kudos, thank you!

# Betreff: Loop problem

Pioneer

Hi,

So, I change a little bit. E.g. I have situation where I have 9 parts and every part have field = 0,45.
Now in 1 row I can have max sum fields >= 2, and so each row.
So I must check how muche parts I can have in 1 rowe, 2row ...
So Column "Field" in Table "DataTable" is "num_parts"*"field every each part"
Column " PartRatio" is the difference: 2 - "Field" from table "DataTable"

I hope that is all clear.

Regards

# Betreff: Loop problem

Gears Phenom

Hi Lucas,

yes, now I got it, thanks

Here's how you can achieve it:

```is
sTab			: object;	--> the Source table "Start"
dTab			: object;	--> the destination table "DataTable"
sRow			: integer;	--> rownumber in "Start"
dRow			: integer;	--> rownumber in "DataTable"
iNumParts		: integer;	--> the number of parts from "Start"
iMaxParts		: integer;	--> the max. number of parts for the next row in "DataTable"
rMaxField		: real;		--> the max. value over all relevant "Field"-columns
rSumPartRatio	: real;		--> for fixing rounding-errors

do
-- get table-objectreferences
sTab	:= Start;
dTab	:= DataTable;

-- delete the destination table and start filling it in row #1
dTab.delete({1,1}..{*,*});
dRow	:= 0;

-- walk through the source-table
for sRow := 1 to sTab.yDim loop
iNumParts		:= sTab["num_parts",sRow];
rMaxField		:= sTab.max({"Field",sRow}..{"Field",sRow});		--> you can add more columns here
rSumPartRatio	:= 0;

-- now split this into multiple rows with a maximum of 2.0 for each row
while iNumParts > 0 loop
-- get value for next row:
-- 	1) iMaxParts cannot be bigger than iNumParts
--	2) iMaxParts*rMaxField cannot be bigger than 2.0
iMaxParts	:= min(iNumParts, floor(2.0 / rMaxField));

-- increment row counter and update the "todo-counter"
dRow		:= dRow + 1;
iNumParts	:= iNumParts - iMaxParts;

-- write a new row into destination table
dTab["name",dRow]		:= sTab["name",sRow];
dTab["num_parts",dRow]	:= iMaxParts;
dTab["Field",dRow]		:= iMaxParts*rMaxField;

-- for the part-ratio, fix rounding-errors with last row
if iNumParts = 0 then
-- this is the last row for this part_name!
dTab["PartRatio",dRow]	:= 1.0 - rSumPartRatio;
else
-- not the last row
dTab["PartRatio",dRow]	:= round(iMaxParts/sTab["num_parts",sRow], 2);
end;

-- update ratio-sum for this part_name
rSumPartRatio			:= rSumPartRatio + dTab["PartRatio",dRow];
end;
next;

end;```

Basically what I did is:

• get a "todo"-counter of the total parts (here: 9) and use a while-loop until the "todo" is zero
• calculate the number of parts for each next row in DataTable with
`iMaxParts	:= min(iNumParts, floor(2.0 / rMaxField));`
• reduce the "todo"-counter accordingly

It looks more complex than it is, but I like to use local variables with (more or less) self-explanatory names, and I've added a check for round-errors, since I don't know for what purpose you will use the column "PartRatio".

Regards,
Alex

___________________________________________________________
Alex Dilg, Consultant at SimPlan AG (www.SimPlan.de)
This post was good and/or helpful to you? Please provide some Kudos, thank you!

# Betreff: Loop problem

Pioneer

This is what I need, thank You Very Much for help

Regards.