Forum Discussion

Kev_B's avatar
Kev_B
Advisor
7 years ago

Counting list output

Really hoping somebody can help.

 

I'm pulling in the number of unread posts in a thread using the following:

SELECT user_context.read FROM messages

 

This gives me a nice list of booleans, I've used ?then(x,y) to convert the booleans into either 0 (read) or 1 (unread). Using a list I'm running through each message from the LiQL query and printing the result. So far I'm ending up with a series of 1's for unread messages and nothing for read messages.

 

What I'd like to do is add all of the outputs together so I can display something like '4 New' as opposed to '1 1 1 1 New'. I feel like I've trawled FreeMarker endlessly with no joy beyond converting the boolean to a number.

 

Here's what I'm working with:

<#list>
   <#assign var1 to get each user_context.read result />
   <#assign var2 to convert these to a number />

   ${var2}
</#list>

I've tried storing the number in var each time and incrementing it, but I end up with '2' then every time, it doesn't add up the actual instances of 1, just adds its value to itself again. I'm sure I'm missing something really obvious, would really appreciate some input.

 

N.B. I know there's a read messages piece in API v1, I'm trying to stick with using v2 if possible as everything else I've built is using v2 so that's what I'm familiar with right now.

  • Kev_B - Can you please share the code where you are adding the number. There might be some mistake in the code. 

    • Kev_B's avatar
      Kev_B
      Advisor

      Hi Tariq,

       

      I'm not currently adding the numbers, so I'm open to suggestions.

       

      I had tried taking the output of var2 and incrementing (var2++), thinking that each time it would print out '1' (3 times in a specific instance), that it would add those together, all it actually did was give me 1 + 1, no matter how many times the result would return if I wasn't adding the numbers.  

       

      I've also trying to declare var2 = 0 at the start of the list and then adding 1 with each successful iteration. It always either came out as 1 or 2, never the number of instances, yet when I do ${var2} it prints out exactly as many times as it gets a result.

      • Kev_B - 

         

        I know you can achieve this using API V2 as well. However, I would suggest you to use API V1 as this approach is good for small amount of messages, however, let say the number of messages is 2000 the list will run 2000 times which will slow down the performance. 

         

        Below code might be helpful, I have tested it and it worked fine, returned correct amount of unread messages. 

        <#assign Messages = rest("2.0", "/search?q=" + "select user_context.read from messages"?url).data.items />
        <#assign newMessage = 0 /> 
        <#list Messages as m>
          <#assign newMessage = newMessage + m.user_context.read?then(0,1) />
        </#list>
        
        New Messages - ${newMessage}