# Sum and count on lists?

I have a string of data:

`"~Donation - \$25~1~25.00~~~~Amalia Jones - 301|~All Recipes~1~15.00~~~~Amalia Jones - 301|~Buttery Caramel~1~18.00~~~~Amalia Jones - 301|897-332~Peanut Butter Cups~1~12.00~~5325~~Bobby Jones - 285|~Chocolate Chip Cookie Dough~1~20.00~~~~Amalia Jones - 301",`

The individual Order Items are delineated by “|” so I can make a list from text that now contains the individual order items

A single item in the list would be

`"~Donation - \$25~1~25.00~~~~Amalia Jones - 301"`

I can then split this list on “~” to get quantity (1), amount (25.00), person (Amalia Jones - 301), etc. as individual elements

If I now want to sum all the amounts and quantities for each person so I end up with the following data available:
Name / QuanitySum / Total
Amalia Jones - 301 / 4 / 85.00
Bobby Jones - 285 / 1 / 12.00

Any suggestions? I could put it into a table, and ask the DB to do the math for me. Does that introduce execution risk by going to the db to insert and then back again for the sums?

Thanks,
Tim

If the data is already parsed, it seems like an overkill to put it into the DB in order the get the sum and counts.

Regards,
Mark

That’s what I thought, but I don’t know how to get the sum in a list.

Perhaps I am missing something complex here, but calculating a sum should go like this:

1. Create a variable X that will accumulate the sum value.
2. Iterate over the list and in the iterator’s step add the current item value to X.

This needs to be dynamic because there is an unknown number of totals as there could be from 1 to n people in the data string.

Your function would be working with a finite data set, wouldn’t it?

The data represented in the string is the items in the order and the Seller who should get credit. There could be:

One to n order items and
one to n sellers

I need a total of all the order item quantities and dollars by Seller.

In another language, I might dynamically set an array key to the seller name and increment up the value that way. I don’t think objects can have dynamic key names in Backendless.

Tim

Objects in Codeless work the same way as objects in JS (this means that dynamic key names are allowed).

Thanks @mark-piller. I was trying to assign dynamic key names while creating an object instead of assigning dynamic object properties.

Tim