You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mike Williams <md...@bigfoot.com> on 2002/06/16 13:06:47 UTC

Re: VTL: Obtain direct reference to first object in collection?

  >>> "lloyd" == lloyd <su...@twilight-systems.com>

  lloyd> Normally, to iterate through a collection I'd do something like this:
  lloyd> #foreach($item in $items)
  lloyd>    ...
  lloyd> #end
  lloyd> 
  lloyd> Is there a way to just get a reference to that first $item without
  lloyd> putting it in a #foreach() ?

There's nothing that will work for all sequences, but if you know $items is
a java.lang.Collection, then

    $items.iterator().next()

should do the trick :-)

-- 
cheers, Mike

If you can read this, thank a teacher.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: VTL: Obtain direct reference to first object in collection?

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 6/16/02 7:06 AM, "Mike Williams" <md...@bigfoot.com> wrote:

>>>> "lloyd" == lloyd <su...@twilight-systems.com>
> 
> lloyd> Normally, to iterate through a collection I'd do something like this:
> lloyd> #foreach($item in $items)
> lloyd>    ...
> lloyd> #end
> lloyd> 
> lloyd> Is there a way to just get a reference to that first $item without
> lloyd> putting it in a #foreach() ?
> 
> There's nothing that will work for all sequences, but if you know $items is
> a java.lang.Collection, then
> 
>   $items.iterator().next()
> 
> should do the trick :-)

Yep - because Velocity can handle several different types of iterative, it's
hard to have 'one way' of getting access.

It might be useful if we had a tool available that used the same approach as
Velocity to deal with iteratives, and then you could ask it for size and
different elements.


-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>