Apologies if this has been asked before but i could not find a straightforward answer.
I have the following code, the application is a third party so the incoming $supernet is provided as a string. So what i am wanting to do it take a IP address and then manipulate it to create a list of subnets based on a template.
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split("\."))
#set ($netmgmt[2] = "$octs[0].$octs[1].$octs[2].$octs[2] 1")
$netmgmt
But this as I expect gives the result "10.10.10.10 1"
I figured out I can do this in a longhand way
- convert the $oct[2] to an integer and store in a variable
- create a temporary variable and store the result of the converted $oct[2] 1
- Then use this to create the string
like
$Integer = $convert.toInteger($oct[2])
#set ($newoct = $Integer 1)
#set ($netmgmt[2] = "$octs[0].$octs[1].$octs[2].$newoct")
but i have a lot of these to create and this will get long winded and confusing.
So i am trying to work out if I can write #set ($octs = $Supernet.split(".")) to store as integers. To be honest just trying to directly convert each array value to int does not seem to work as based on what i have found on line. I expected the below would result in "$oct1 = 10" but it does not?
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split("\."))
#set ($oct1 = $convert.toInteger($oct[0])
New to Velocity any help appreciated.
CodePudding user response:
You should check which version of Velocity-Tools you are using, and which tools are available in the provided context.
Since Velocity-Tools 3.1, for instance, $convert.toInteger()
has been deprecated in favor of $math.toInteger()
. You can check if those tools are present in the context just by displaying $convert
and $math
.
The following code works for me with Velocity 1.7 Tools 2.0, as long as with Velocity 2.X Tools 3.x:
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split('\.'))
#set ($last = $math.toInteger($octs[2]) - 1)
#set ($ip = "${octs[0]}.${octs[1]}.${octs[2]}.$last")
$ip