Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to determine length of array in a Velocity User Macro ?

Garry May July 3, 2024 edited

This should be a really simple thing to do - determine the length of an array in Confluence/Velocity - but I'm struggling to figure it out!

I've tried 3 ways to find the length of an array (shown below) and the only way that works is via Method 3 (a horrible loop?)  

N.B. the $list is a reference to (hopefully) the ListTool library, is it included in Confluence's version of Velocity? If not, how can I add it?

Can someone slap me upside the head and tell me what's wrong here?

Thanks

 

The Example Code

#set($keys = "AAA,BBB,CCC,DDD") 

## Method 1
#set($arrayKeys = $keys.split(","))
#set( $countBySize = $arrayKeys.size() )

## Method 2
#set( $countbyList = $list.size($arrayKeys))

## Method 3
#set($countByKeys = 0)
#foreach ($key in $arrayKeys)
#set($countByKeys = $countByKeys + 1)
#end

 

The Results as Rendered

vm-code-array-size.jpg

N.B. numKeys in the table should be countByKeys

1 answer

1 vote
Adriian Semotiuk _Wombats Corp_
Contributor
July 8, 2024 edited

Hello @Garry May , 
First of all Velocity Templates supports arrays as type of variable , so you can initialize array as this, so you would not be splitting string into array

#set($array = ["AAA","BBB","CCC","DDD"])

 And since Velocity Template supports interaction with Collection API VTL Reference , we can use method size() on all Collection classes, so code like this should work

#set($array = ["AAA","BBB","CCC","DDD"])
Size of an array: $array.size()

Hope this will resolve your issue

Garry May July 9, 2024

Hi Adriian, thanks for getting back to me.  The probably I have is a bit misleading in that I'm getting a comma separated string of keys from a User Macro parameter, i.e. a single string like "A,B,C" which is different to how you've initialised your array. I use the split function to return the array of keys....but the subsequent call to the size() function doesn't seem to be recognised when I evaluate it?  What am I missing?

The split function returns ($arrayKeys, as per the table of results)

"[Ljava.lang.string:@xxxxx" Is this an array reference or simple a long string?

Thanks

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.9.3
TAGS
atlassian, out of office, loom, confluence, async collaboration, meeting recaps, remote teamwork, team alignment, knowledge sharing, AI-powered notes, productivity tools, workforce continuity, teamwork collection

Put your meeting notes on autopilot with Loom + Confluence

AI-powered meeting notes keep work moving even if you’re out of the office. Enjoy your well-deserved time off and return refreshed, confident you’ll catch up in no time.

Learn more
AUG Leaders

Atlassian Community Events