You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ryan Lea <Ry...@Rightmove.co.uk> on 2004/01/05 18:22:21 UTC

Setting values in an array list

Hi all,

This is potentially a daft question, BUT I couldn't find anything in the
users list.  Is there a way to set values of an array from within a foreach
loop?

Eg:

#foreach ( $i in [0..50] )
   ## somehow set position $i in the array to the corresponding value
#end

OR

#set ( $array1 = ['a', 'b', 'c', 'd', 'e'] )
#set ( $array2 = ['z', 'y', 'x', 'w', 'v'] )

#foreach ( $value in $array1 )
   ## somehow set the value in the array2 as the same as in array1
#end

Cheers

Ryan


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs service. 
________________________________________________________________________

Re: Setting values in an array list

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
Additionallym, assuming that you really wanted to know how to add in  
the #foreach loop :

#set($array1 = [5,6,7,8,9,10])
#set($array2 = [])

#foreach($val in $array1)
   $array2.add($val * 2)
#end

geir


On Jan 5, 2004, at 1:41 PM, Will Glass-Husain wrote:

> Hi Ryan,
>
> #set creates an ArrayList, so you can use the List methods.
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
>
> Try
>
> #set ( $array1 = ['a', 'b', 'c', 'd', 'e'] )
> #set ( $array2 = [] )
> $array2.addAll($array1)
>
> The one caveat is that the addAll method will display "true" as it has  
> a
> return value.  To remove this do
>
> #set ($result = $array2.addAll($array1) )
>
> WILL
>
> ----- Original Message -----
> From: "Ryan Lea" <Ry...@Rightmove.co.uk>
> To: <ve...@jakarta.apache.org>
> Sent: Monday, January 05, 2004 9:22 AM
> Subject: Setting values in an array list
>
>
>> Hi all,
>>
>> This is potentially a daft question, BUT I couldn't find anything in  
>> the
>> users list.  Is there a way to set values of an array from within a
> foreach
>> loop?
>>
>> Eg:
>>
>> #foreach ( $i in [0..50] )
>>    ## somehow set position $i in the array to the corresponding value
>> #end
>>
>> OR
>>
>> #set ( $array1 = ['a', 'b', 'c', 'd', 'e'] )
>> #set ( $array2 = ['z', 'y', 'x', 'w', 'v'] )
>>
>> #foreach ( $value in $array1 )
>>    ## somehow set the value in the array2 as the same as in array1
>> #end
>>
>> Cheers
>>
>> Ryan
>>
>>
>> ______________________________________________________________________ 
>> __
>> This email has been scanned for all viruses by the MessageLabs  
>> service.
>> ______________________________________________________________________ 
>> __
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


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


Re: Setting values in an array list

Posted by Will Glass-Husain <wg...@forio.com>.
Hi Ryan,

#set creates an ArrayList, so you can use the List methods.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

Try

#set ( $array1 = ['a', 'b', 'c', 'd', 'e'] )
#set ( $array2 = [] )
$array2.addAll($array1)

The one caveat is that the addAll method will display "true" as it has a
return value.  To remove this do

#set ($result = $array2.addAll($array1) )

WILL

----- Original Message ----- 
From: "Ryan Lea" <Ry...@Rightmove.co.uk>
To: <ve...@jakarta.apache.org>
Sent: Monday, January 05, 2004 9:22 AM
Subject: Setting values in an array list


> Hi all,
>
> This is potentially a daft question, BUT I couldn't find anything in the
> users list.  Is there a way to set values of an array from within a
foreach
> loop?
>
> Eg:
>
> #foreach ( $i in [0..50] )
>    ## somehow set position $i in the array to the corresponding value
> #end
>
> OR
>
> #set ( $array1 = ['a', 'b', 'c', 'd', 'e'] )
> #set ( $array2 = ['z', 'y', 'x', 'w', 'v'] )
>
> #foreach ( $value in $array1 )
>    ## somehow set the value in the array2 as the same as in array1
> #end
>
> Cheers
>
> Ryan
>
>
> ________________________________________________________________________
> This email has been scanned for all viruses by the MessageLabs service.
> ________________________________________________________________________


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