You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Dimitrios Kolovos <ds...@hotmail.com> on 2005/01/14 11:17:51 UTC

Remove last comma...

Hi all,

When iterating a collection/array it is usual that you add something after 
each iteration except for the last.

e.g.

#foreach ($i in [1..10])$i,#end

produces

1,2,3..10,

The only way I know in which I can omit the last comma is to use 
velocityount. Nevertheless, I think it would be more elegant if there was a 
variable (e.g. $output) in the context that represented the so far produced 
text. Then I could call a method like $output.chop(n) and chop the last n 
characters.

Any thoughts?

Dimitrios Kolovos 

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


Re: Remove last comma...

Posted by Shinobu Kawai Yoshida <sh...@gmail.com>.
Hi Dimitrios,

> Thank you all for your replies. The best solution in my case is the inline
> evaluation $foo="#foreach...#end" Shinobu suggested. What I would like to
> ask is whether I can trigger in some way the re-evaluation of $foo.

Do you mean something like the (View)RenderTool?
   http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/RenderTool.html
   http://jakarta.apache.org/velocity/tools/view/ViewRenderTool.html

## Hey, home come there's an xdoc for ViewRenderTool, but not RenderTool?

Best regards,
-- Shinobu

--
Shinobu "Kawai" Yoshida <sh...@gmail.com>

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


Re: Remove last comma...

Posted by Dimitrios Kolovos <ds...@gmail.com>.
Thank you all for your replies. The best solution in my case is the inline 
evaluation $foo="#foreach...#end" Shinobu suggested. What I would like to 
ask is whether I can trigger in some way the re-evaluation of $foo.

Dimitrios

----- Original Message ----- 
From: "Wiebe de Jong" <wi...@shaw.ca>
To: "'Velocity Users List'" <ve...@jakarta.apache.org>
Sent: Monday, January 17, 2005 7:22 PM
Subject: RE: Remove last comma...


> Yes. This is the standard pattern I use for displaying lists:
>
> if (list not null)
>  display header
>  display element[1]
>  loop (each element in list)
>    display separator
>    display element[n]
>  display footer
>
> Having a check before the loop depends on how the loop is done. In Java, I
> use a while loop, so no check is required. I don't know how the foreach 
> loop
> in Velocity would handle an empty list.
>
> Wiebe de Jong
>
> -----Original Message-----
> From: Will Glass-Husain [mailto:wglass@forio.com]
> Sent: Monday, January 17, 2005 10:55 AM
> To: Velocity Users List
> Subject: Re: Remove last comma...
>
> Then you need an if statement to ensure the list has at least 2 elements.
>
> WILL
>
> ----- Original Message ----- 
> From: "Wiebe de Jong" <wi...@shaw.ca>
> To: "'Velocity Users List'" <ve...@jakarta.apache.org>
> Sent: Monday, January 17, 2005 10:04 AM
> Subject: RE: Remove last comma...
>
>
>> How about process the fist line separately, then all the rest.
>>
>> $1
>> #foreach ($i in [2..10],$i#end
>>
>> Will product 1,2,3..10
>>
>> Wiebe de Jong
>>
>> -----Original Message-----
>> From: Dimitrios Kolovos [mailto:dskolovos@hotmail.com]
>> Sent: Friday, January 14, 2005 2:18 AM
>> To: Velocity Users List
>> Subject: Remove last comma...
>>
>> Hi all,
>>
>> When iterating a collection/array it is usual that you add something 
>> after
>> each iteration except for the last.
>>
>> e.g.
>>
>> #foreach ($i in [1..10])$i,#end
>>
>> produces
>>
>> 1,2,3..10,
>>
>> The only way I know in which I can omit the last comma is to use
>> velocityount. Nevertheless, I think it would be more elegant if there was
>> a
>> variable (e.g. $output) in the context that represented the so far
>> produced
>> text. Then I could call a method like $output.chop(n) and chop the last n
>> characters.
>>
>> Any thoughts?
>>
>> Dimitrios Kolovos
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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


RE: Remove last comma...

Posted by Wiebe de Jong <wi...@shaw.ca>.
Yes. This is the standard pattern I use for displaying lists:

if (list not null)
  display header
  display element[1]
  loop (each element in list)
    display separator
    display element[n]
  display footer

Having a check before the loop depends on how the loop is done. In Java, I
use a while loop, so no check is required. I don't know how the foreach loop
in Velocity would handle an empty list.

Wiebe de Jong

-----Original Message-----
From: Will Glass-Husain [mailto:wglass@forio.com] 
Sent: Monday, January 17, 2005 10:55 AM
To: Velocity Users List
Subject: Re: Remove last comma...

Then you need an if statement to ensure the list has at least 2 elements.

WILL

----- Original Message ----- 
From: "Wiebe de Jong" <wi...@shaw.ca>
To: "'Velocity Users List'" <ve...@jakarta.apache.org>
Sent: Monday, January 17, 2005 10:04 AM
Subject: RE: Remove last comma...


> How about process the fist line separately, then all the rest.
>
> $1
> #foreach ($i in [2..10],$i#end
>
> Will product 1,2,3..10
>
> Wiebe de Jong
>
> -----Original Message-----
> From: Dimitrios Kolovos [mailto:dskolovos@hotmail.com]
> Sent: Friday, January 14, 2005 2:18 AM
> To: Velocity Users List
> Subject: Remove last comma...
>
> Hi all,
>
> When iterating a collection/array it is usual that you add something after
> each iteration except for the last.
>
> e.g.
>
> #foreach ($i in [1..10])$i,#end
>
> produces
>
> 1,2,3..10,
>
> The only way I know in which I can omit the last comma is to use
> velocityount. Nevertheless, I think it would be more elegant if there was 
> a
> variable (e.g. $output) in the context that represented the so far 
> produced
> text. Then I could call a method like $output.chop(n) and chop the last n
> characters.
>
> Any thoughts?
>
> Dimitrios Kolovos
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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


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


Re: Remove last comma...

Posted by Will Glass-Husain <wg...@forio.com>.
Then you need an if statement to ensure the list has at least 2 elements.

WILL

----- Original Message ----- 
From: "Wiebe de Jong" <wi...@shaw.ca>
To: "'Velocity Users List'" <ve...@jakarta.apache.org>
Sent: Monday, January 17, 2005 10:04 AM
Subject: RE: Remove last comma...


> How about process the fist line separately, then all the rest.
>
> $1
> #foreach ($i in [2..10],$i#end
>
> Will product 1,2,3..10
>
> Wiebe de Jong
>
> -----Original Message-----
> From: Dimitrios Kolovos [mailto:dskolovos@hotmail.com]
> Sent: Friday, January 14, 2005 2:18 AM
> To: Velocity Users List
> Subject: Remove last comma...
>
> Hi all,
>
> When iterating a collection/array it is usual that you add something after
> each iteration except for the last.
>
> e.g.
>
> #foreach ($i in [1..10])$i,#end
>
> produces
>
> 1,2,3..10,
>
> The only way I know in which I can omit the last comma is to use
> velocityount. Nevertheless, I think it would be more elegant if there was 
> a
> variable (e.g. $output) in the context that represented the so far 
> produced
> text. Then I could call a method like $output.chop(n) and chop the last n
> characters.
>
> Any thoughts?
>
> Dimitrios Kolovos
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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


RE: Remove last comma...

Posted by Wiebe de Jong <wi...@shaw.ca>.
How about process the fist line separately, then all the rest.

$1
#foreach ($i in [2..10],$i#end

Will product 1,2,3..10

Wiebe de Jong

-----Original Message-----
From: Dimitrios Kolovos [mailto:dskolovos@hotmail.com] 
Sent: Friday, January 14, 2005 2:18 AM
To: Velocity Users List
Subject: Remove last comma...

Hi all,

When iterating a collection/array it is usual that you add something after 
each iteration except for the last.

e.g.

#foreach ($i in [1..10])$i,#end

produces

1,2,3..10,

The only way I know in which I can omit the last comma is to use 
velocityount. Nevertheless, I think it would be more elegant if there was a 
variable (e.g. $output) in the context that represented the so far produced 
text. Then I could call a method like $output.chop(n) and chop the last n 
characters.

Any thoughts?

Dimitrios Kolovos 

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


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