You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by kuroiem <eh...@gmail.com> on 2007/06/06 21:37:23 UTC

Initializing Array (without declaring contents and without creating java helper method)

Is there a way to initialize an array using velocity code without having to
declare the contents of the array and without having to create a java helper
method?

Equivalent in java would be: 
int size = 10;
int[] array= new int[size];

What I want to write in velocity:
// I want to set the length of $array to $size, where $size can be any
number
#set( $size = $JavaHelper.getSize() )
#set( $array = $size )
#foreach( $key in $array )
#end

Right now the above velocity code would not work because $array is not an
array, it is a scalar equal to $size. Thus, the foreach loop would never be
entered. I would have to write it like this:
//What I have to write in velocity (to make it work):
// I want to set the length of $array to $size, where $size can be any
number (in this case it is 10)
#set( $size = $JavaHelper.getSize()  )
//Create $array to size of $size
#set( $array = ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1"...])" )
#foreach( $key in $array )
#end

However the above code is static. I would have to manually enter the array
contents for every possible number. I know you could create a newArray(int
size) method in Java and call that from within the velocity code, but I was
just wondering if there was a way to do this with purely velocity code. 

Thanks for the help! :)
-- 
View this message in context: http://www.nabble.com/Initializing-Array-%28without-declaring-contents-and-without-creating-java-helper-method%29-tf3879957.html#a10995743
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Initializing Array (without declaring contents and without creating java helper method)

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

There's also the range operator:

http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#range_operator

You can do something like:

#set($size = whatever)
#foreach($i in 0..$size)
    // body gets evaluated $size times
#end

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGaBut9CaO5/Lv0PARAs2SAJ0aKIU6/N4Sm3sARoh/uH5lwgnwxACfZgU1
pz7kHNBlTFNE/KBc+dou1io=
=JNUy
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Initializing Array (without declaring contents and without creating java helper method)

Posted by Will Glass-Husain <wg...@gmail.com>.
Actually, Velocity works with List objects, not Arrays.

#set($alist = {10, 20, 30, 40, 50})

#foreach($item in $alist)
   $item
#end

works.  You can also call any List method (e.g. add or set).  Does this
help?

WILL

On 6/6/07, kuroiem <eh...@gmail.com> wrote:
>
>
> Is there a way to initialize an array using velocity code without having
> to
> declare the contents of the array and without having to create a java
> helper
> method?
>
> Equivalent in java would be:
> int size = 10;
> int[] array= new int[size];
>
> What I want to write in velocity:
> // I want to set the length of $array to $size, where $size can be any
> number
> #set( $size = $JavaHelper.getSize() )
> #set( $array = $size )
> #foreach( $key in $array )
> #end
>
> Right now the above velocity code would not work because $array is not an
> array, it is a scalar equal to $size. Thus, the foreach loop would never
> be
> entered. I would have to write it like this:
> //What I have to write in velocity (to make it work):
> // I want to set the length of $array to $size, where $size can be any
> number (in this case it is 10)
> #set( $size = $JavaHelper.getSize()  )
> //Create $array to size of $size
> #set( $array = ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1"...])" )
> #foreach( $key in $array )
> #end
>
> However the above code is static. I would have to manually enter the array
> contents for every possible number. I know you could create a newArray(int
> size) method in Java and call that from within the velocity code, but I
> was
> just wondering if there was a way to do this with purely velocity code.
>
> Thanks for the help! :)
> --
> View this message in context:
> http://www.nabble.com/Initializing-Array-%28without-declaring-contents-and-without-creating-java-helper-method%29-tf3879957.html#a10995743
> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com