question

Mohamed Slama avatar image
0 Likes"
Mohamed Slama asked Mohamed Slama commented

use the value of an index (i) of a loop and put it in the title of a label

Hi everybody,

In my model, I want to use the index of my loop in the title of a label. Could you please help me to do that ?

for( i=1 ; i<=400 ; i++)
{
  if(table[i][2] !=0)
  {
    current.machine"i" = table[i][3];
  }
}

I want to use the value of the index 'i' into the title of a label.

Choose One
labelsloopvalueindex
5 |100000

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

1 Answer

Mischa Spelt avatar image
1 Like"
Mischa Spelt answered Mohamed Slama commented

You cannot do this with the dot notation, you need to explicitly construct the name of the label:

for( i=1 ; i<=400 ; i++)
{
  if(table[i][2] !=0)
  {
    string labelName = "machine" + numtostring(i, 0, 0);
    current.labels.assert(labelName).value = table[i][3];
  }
}

Alternatively, can I suggest that you use a single array-valued label? For example, something like:

current.machines = [];
for( i=1 ; i<=400 ; i++)
{
  if(table[i][2] !=0)
  {
    current.machines.push(table[i][3]);
  }
}
· 3
5 |100000

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

Mohamed Slama avatar image Mohamed Slama commented ·

@Mischa Spelt

Thank you for your answer but when I tried it it returns this error:

"Invalid type for left side of assignment operation. Must be valid l-value. Type is treenode".

for this line:

"current.labels.assert(labelName) = table[i][3];"

Here is my code:

for(int i=1;i<= table2[7][2];i++)

{

if(table2[i+7][3]!=0)

{

string labelName = "bol" + numtostring(i+7, 0, 0);

current.labels.assert(labelName) = table2[i+7][3];

}

}

0 Likes 0 ·
Mischa Spelt avatar image Mischa Spelt Mohamed Slama commented ·

Sorry, my bad, there has to be an extra ".value" after the assert. See my updated post.

0 Likes 0 ·
Mohamed Slama avatar image Mohamed Slama Mischa Spelt commented ·

thank you very much :)

0 Likes 0 ·