velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: How can I break a Foreach loop?
Date Thu, 09 Oct 2003 11:09:38 GMT
Next are some hints on how to break a #foreach loop in velocity.
There are many ways to skin a cat ;)

For the most cases a solution simlar to Mike's one does the
trick - when the count is not very large. A simple #if
with the proper condition just skips the body processing
instead of breaking out:
#foreach( $item in $yourArray )
   #if( ($velocityCount >= 10) && ($velocityCount < 20) )
     Item: $item.getValue()

The most powerful solution (in the case you know the API of the
collection being iterated) is to use the specific form:
#set( $start = 10 )
#if( $start >= $yourArray.size )
   #set( $end = 20 )
   #if( $end > $yourArray.size )
     #set( $end = $yourArray.size )
   #foreach( $index in [${start}..${end}] )
     ## explicitly get the object from the collection
     Item: $yourArray.get($index)
## Note tat this will not work with plain arrays, for which
## you could write a simple wrapper providing breakable iterator
## functionality as stated next.
## Also note that it is not recommended to use very large ranges,
## e.g. [1000..1000000], since velocity allocates an array of
## such a size behind the scene. Here the breakable iterator
## is the better solution as presented next.

A further solution (a previous thread in the vel-user list has
treated this) is to create a simple tool, lets call it
BreakableIterator, possibly reusing velocity's iterator introspection
mechanism, and add a break() method that tells it to return false
for the next hasNext() call. The the loop would look like:
#foreach( $item in $iteratorTool.getBreakableIterator($yourArray) )
   Item: $item.getValue()
   #if( $velocityCount >= 10 )## or any other condition that flags the last
## Note here - you could enhance the iterator to start off
## from a specific index, aka a method like
## getBreakableIterator($yourArray, $startIndex)

Another usable approach for this is the IteratorTool in the
velocity-tools subproject:

An expert could modify the velocity sources, adding a
#break() directive to the #foreach() implementation (likely
will require a grammar update). Is this desireable?
Velocity's policy here has been to KISS (Keep it Simply Simple).

Hope this helps you and other wise users that search the list
for solutions. Maybe this text can be taken into the users guide.


theFenix wrote:
> Hello:
> I'm a VM programmer and I search for a method to break a loop when I find an occurrence
in a Vector object.
> Something like the "break" statement in Java and C++ or another way to simulate this.
> I hope that you can help me.
> Thank you very much.
> Rodrigo González Kirkman
> Santiago - Chile.
> - Tu correo web gratis, sin banners y popups publicitarios en
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

:) Christoph Reck

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message