split() and Strings. Problem reading the resulting array

23 views
Skip to first unread message

Guy Verville

unread,
Jan 14, 2020, 9:48:57 AM1/14/20
to Scroll Viewport Developers
Hi,
I'm not that familiar with Velocity. We try to make a simple split over a string like this:

#set ($myString = "This|is|my|dummy|text") 
#set ($theSplit = $myString.split("[|]"))

#foreach($id in $theSplit)
$id<br>
#end

This works. But...

$theSplit.get(0) returns an exception on Null...

I have read that .get() if available only for Velocity 1.7 and above. We just bought Viewport. What is the version of Velocity in it?

Message has been deleted

Guy Verville

unread,
Jan 14, 2020, 2:11:36 PM1/14/20
to Scroll Viewport Developers
I just read that Viewport uses Velocity 1.6. What is the workaround then?

Steffen Burzlaff (K15t)

unread,
Jan 15, 2020, 2:41:33 AM1/15/20
to Scroll Viewport Developers
Hi,

thanks for reaching out.

Sure there is a way to solve this, the problem here is that you currently don't have an array of strings you access.
So what you need to do is to create an array and add the $id into it. In Velocity adding a value to an array returns a boolean string and outputs it in the site which you probably don't want to have.
To suppress that behaviour you can create a dummy variable (I call that always $swallow) so that there is no output.

here is the adapted code of yours:

#set ($myString = "This|is|my|dummy|text")
#set ($theSplit = $myString.split("[|]"))
#set ($myStringSet = [])

#foreach($id in $theSplit)
  #set ($swallow = $myStringSet.add($id)
#end

$myStringSet
.get(0)

Let me know if that fixed the problem.

Thanks and have a nice day.

Cheers,
Steffen

Guy Verville

unread,
Jan 15, 2020, 10:24:04 AM1/15/20
to Scroll Viewport Developers
That was the part missing for me. Thank you!


Reply all
Reply to author
Forward
0 new messages