velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph.R...@dlr.de
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()
   #end
#end

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 )
   #end
   #foreach( $index in [${start}..${end}] )
     ## explicitly get the object from the collection
     Item: $yourArray.get($index)
   #end
#end
## 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
     $item.break()
   #end
#end
## 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:
http://cvs.apache.org/viewcvs.cgi/jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/IteratorTool.java?rev=HEAD&content-type=text/vnd.viewcvs-markup


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.

Cheers,
Christoph


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 Xasa.com
>   http://www.xasa.com/mail/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 
> 

-- 
:) Christoph Reck


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


Mime
View raw message