sum value in array

sum value in array

jason_p2
Not applicable
660 Views
4 Replies
Message 1 of 5

sum value in array

jason_p2
Not applicable

[ FlexSim 19.2.3 ]

I have a token for each warehouse order. I need to get the total quantity per order, so I need to sum up the array token.Qty. How do I create a label that sums the array for example token.OrderQty = 11

23228-capture87.png

0 Likes
Accepted solutions (1)
661 Views
4 Replies
Replies (4)
Message 2 of 5

regan_blackett
Autodesk
Autodesk
Accepted solution

You could copy this and paste it into the Code Editor for the Value field on an Assign Labels activity:

Array theArray = token.Qty;
double aggregate;

for(int i = 1; i <= theArray.length; i++)
    aggregate += theArray;
    
return aggregate;
Message 3 of 5

joerg_vogel_HsH
Mentor
Mentor

By a table query over the array length with a SUM clause

Array qty = [12,1.23,3.56];
double sum = Table.query("SELECT SUM($2) FROM $1",
      			    /*$1*/ qty.length,
      			    /*$2*/ qty[$iter(1)]
  			)[1][1];
print(sum);
0 Likes
Message 4 of 5

rcsrcs31
Explorer
Explorer

Hello, 

 

I know this comment is a bit old, but I was wondering if you could explain further in detail the following part of the logic: 

 

FROM $1
/*$1*/ qty.length, 
/*$2*/ qty[$iter(1)]

 I do not fully understand how you can you read from an integer. I tried to go through the documentation, but I wasn't able to fully understand it.

 

Kind regards.

0 Likes
Message 5 of 5

joerg_vogel_HsH
Mentor
Mentor

Table .query("SELECT 'something' AS a_thing FROM $1",4).cloneTo(Table("myTable"));

results in myTable of four rows containing each cell a string of something. Header is a_thing.
$iter is Flexsim SQL Clause to iterate over each discrete structure from 1 to last element. If it is an integer like 4 then it is 1,2,3,4.
Please look for first given answers here at this forum about the keyword 'iter'. 

See also Advanced Query Techniques in Manual.

0 Likes