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
N.B. numKeys in the table should be countByKeys
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.