Sum structure values

pho's picture

Hi,

I have the following structure:

0 0= "content": 1 1= "name": shares

1 0= "content": 2 1= "name": shares

3 0= "content": 0 1= "name": shares

4 0 = "content": 1 1= "name": shares

How can I sum the values in content so I get 4 as a result?

Achim Breidenbach's picture
Re: Sum structure values

To my knowledge this is only possible with JavaScript (native in QC) or LUA Script (http://docs.boinxtv.com/lua_patch).

Sala28's picture
Re: Sum structure values

I found this comp. on my computer... I don't remember who is the author. I think it's what you are looking for.

PreviewAttachmentSize
Structure Addition.qtz6.62 KB

pho's picture
Re: Sum structure values

Sala28 wrote:
I found this comp. on my computer... I don't remember who is the author. I think it's what you are looking for.

Thanks, and sorry for my poor text formatting. I think i'd better upload a picture.

I can't get your comp to sum the values of "shares" item. How can I do that?

Note that not all members contain that item and that that item could be any number, not just 1.

PreviewAttachmentSize
Captura de ecrã 2014-03-17, às 14.50.37.png
Captura de ecrã 2014-03-17, às 14.50.37.png127.14 KB

Achim Breidenbach's picture
Re: Sum structure values

@Sala28: Interesting: I didn't know that an Iterator can have an output, thought.

@pho: You have to put a "Structure Key Member" patch (with key "content") between "Structure Index Member" and "+" patch to get your "content"-value.

pho's picture
Re: Sum structure values

Achim Breidenbach wrote:
@Sala28: Interesting: I didn't know that an Iterator can have an output, thought.

@pho: You have to put a "Structure Key Member" patch (with key "content") between "Structure Index Member" and "+" patch to get your "content"-value.

Thanks, it works perfectly. I need to look into iterators better.

BTW, do you know how to sort the structure in descending order by the "shares" number?

zuvala's picture
Re: Sum structure values

Use the structure sort patch and put an expression mark in front of the value for sorting descriptor. ie: !shares

PreviewAttachmentSize
Bildschirmfoto 2014-03-18 um 19.54.51.png
Bildschirmfoto 2014-03-18 um 19.54.51.png24.47 KB

pho's picture
Re: Sum structure values

zuvala wrote:
Use the structure sort patch and put an expression mark in front of the value for sorting descriptor. ie: !shares

the sorting is wrong. It is sorting by the first digit only, then by the first two and so on.

this is what I get:

PreviewAttachmentSize
Captura de ecrã 2014-03-19, às 17.15.05.png
Captura de ecrã 2014-03-19, às 17.15.05.png103.59 KB