question

Anggoro P avatar image
0 Likes"
Anggoro P asked Phil BoBo edited

Multiple counter in a for looping

Hi, I want to make a for loop using 2 counter at the same time. I made some code but it doesn't work. Any suggestion?

for (l = 1; l <= Table("DailyDemand%").numRows; l++)
{
    for (j = 1, k = l; j <= Array(CO_intWeeklyDemand).length, k <= Array(CO_day).length; j ++, k +=7)
    {
        CO_day[k] = round((Table("DailyDemand%")[j]["CO"]/100) * CO_intWeeklyDemand[j]);
    }
}
Choose One
flexscriptflexscript error
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Anggoro P avatar image Anggoro P commented ·
0 Likes 0 ·
Phil BoBo avatar image
2 Likes"
Phil BoBo answered Phil BoBo edited

Move the second variable initialization outside of the for() loop, use && to combine your condition, and put the second variable incrementer at the end of the for() loop.

for (l = 1; l <= Table("DailyDemand%").numRows; l++)
{
    k = l;
    for (j = 1; j <= Array(CO_intWeeklyDemand).length && k <= Array(CO_day).length; j++)
    {
        CO_day[k] = round((Table("DailyDemand%")[j]["CO"]/100) * CO_intWeeklyDemand[j]);
        k+=7;
    }
}

Also, you should use good variable names instead of single letters to make your code more readable and debug-able.

For example, you use the variable l (the letter) which is almost identical to a 1 (the number).

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Cameron Pluim avatar image
0 Likes"
Cameron Pluim answered

I believe this syntax is not allowed within FlexSim. You will need to explicitly increment and check for conditions within the for loop like so:

int l = 1;
int j = 1;
int k = 1;


for (l = 1; l <= Table("DailyDemand%").numRows; l++)
{
    for (j = 1; j <= Array(CO_intWeeklyDemand).length; j ++)
    {
        CO_day[k] = round((Table("DailyDemand%")[j]["CO"]/100) * CO_intWeeklyDemand[j]);
	if(k <= Array(CO_day).length) 
	{
	    break;
	}
	k +=7;
} }

Maybe you could post this as an idea for improvement to allow this type of syntax. Although I'm not sure how common this syntax is in other programming languages.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Steven Hamoen avatar image
0 Likes"
Steven Hamoen answered Anggoro P edited

@Anggoro P would be helpful if you would also describe or show what error you get and when (while running or during writing the code).

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Anggoro P avatar image Anggoro P commented ·

@steven.hamoen

it says:

line 21 and 48: unexpected "," expecting ";" . Those lines are exactly the same code, I just replaced the variable name

line 55: unexpected ")" expecting ";" check for proper number of parentheses. This error is weird because there is no error on similar line (line 28)

0 Likes 0 ·
a.jpg (180.1 KiB)
Steven Hamoen avatar image
0 Likes"
Steven Hamoen answered Phil BoBo converted comment to answer

first thing I see is that you don't define l, j and k so it should be:

for( int l =....
and for( int j = 1, int k = 1 ..

(unless you have defined them above your code but that I can't see)

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Anggoro P avatar image Anggoro P commented ·

Hi @steven.hamoen maybe it's better to paste all code her. I still got there error

Table current = param(1); //Data node
{ // ************* PickOption Start ************* //
/***popup:CodeSnippet*/
/***tag:description*//**Code Snippet*/
/***tag:snippet*//**/
Array CO_intWeeklyDemand = Array(7);
Array CO_day = Array(49);
int i;
int j;
int k;
int l;
int m;

for (i = 1; i <= Array(CO_intWeeklyDemand).length; i++)
{
    CO_intWeeklyDemand[i] = fabs(johnsonbounded(Table("ShipzoneData")["WeeklyDemand_Par1"]["CO"],Table("ShipzoneData")["WeeklyDemand_Par2"]["CO"],Table("ShipzoneData")["WeeklyDemand_Par3"]["CO"],Table("ShipzoneData")["WeeklyDemand_Par4"]["CO"],Table("ShipzoneData")["Stream"]["CO"]));
}

for (l = 1; l <= Table("DailyDemand%").numRows; l++)
{
    for (j = 1, k = l; j <= Array(CO_intWeeklyDemand).length, k <= Array(CO_day).length; j ++, k +=7)
    {
        CO_day[k] = round((Table("DailyDemand%")[j]["CO"]/100) * CO_intWeeklyDemand[j]);
    }
}


for (int m = 1; m <= Array(CO_day).length; m++)
{
    settablenum("DailyDemand",m,1,CO_day[m]);
}

Array BE_intWeeklyDemand = Array(7);
Array BE_day = Array(49);
int i1;
int j1;
int k1;
int l1;
int m1;

for (i1 = 1; i1 <= Array(BE_intWeeklyDemand).length; i1++)
{
    BE_intWeeklyDemand[i1] = fabs(johnsonbounded(Table("ShipzoneData")["WeeklyDemand_Par1"]["BE"],Table("ShipzoneData")["WeeklyDemand_Par2"]["BE"],Table("ShipzoneData")["WeeklyDemand_Par3"]["BE"],Table("ShipzoneData")["WeeklyDemand_Par4"]["BE"],Table("ShipzoneData")["Stream"]["BE"]));
}

for (l1 = 1; l1 <= Table("DailyDemand%").numRows; l1++)
{
    for (j1 = 1, k1 = l; j1 <= Array(BE_intWeeklyDemand).length, k1 <= Array(BE_day).length; j1 ++, k1 +=7)
    {
        BE_day[k1] = round((Table("DailyDemand%")[j1]["BE"]/100) * BE_intWeeklyDemand[j1]);
    }
}


for (m1 = 1 ; m1 <= Array(BE_day).length; m1++)
{
    settablenum("DailyDemand",m1,1,BE_day[m1]);
}



/**/
; // leave a no-op statement in case they leave it empty
} // ******* PickOption End ******* //

<br>
0 Likes 0 ·
a.jpg (31.2 KiB)