Wouldn't something like: #set ($list = []) #foreach($employee in $employeeList) #set ($dept = $employee.getDepartment()) #if (! $list.contains($dept)) { #set ($dummy = $list.add($dept)) #endif #end
$list would then contain a list of all the unique department numbers (for example) John Withers -----Original Message----- From: Mike Kienenberger [mailto:[EMAIL PROTECTED] Sent: Monday, October 03, 2005 4:04 PM To: Velocity Users List Subject: Re: Clean way to iterate over a distinct set of items? Thanks. Creating a HashSet was my first thought at well, but I couldn't remember the syntax, nor under what situations it was allowed :) What I have works, and while it's ugly, it's good enough :) If I'd seen the newInstance() example first, I would have went with that, though. And congrats, Nathan! Your addition to the core velocity contributors has been a long time coming! On 10/3/05, Nathan Bubna <[EMAIL PROTECTED]> wrote: > well, since Will's proposed security enhancements haven't yet arrived, > why not do something like: > > #set( $set = $anything.class.forName('java.util.HashSet').newInstance() ) > #foreach( $thing in $list )## > $!set.add($thing.subThing)## > #end > #foreach( $subThing in $set ) > $subThing > #end > > :) > > On 10/3/05, Mike Kienenberger <[EMAIL PROTECTED]> wrote: > > Yes, the problem is that I don't have access to the context in this > > situation, only to the template. I needed a 100% VTL solution since I > > couldn't add anything to the context. > > > > On 10/3/05, Will Glass-Husain <[EMAIL PROTECTED]> wrote: > > > Myself, I usually do this in the controller and pass the unique list to > > > Velocity. More often I have a single object (e.g. $form) and just make a > > > method that translates: > > > > > > $form.employeeList > > > $form.uniqueEmployeeList > > > > > > each method drawing from the same original data source. > > > > > > WILL > > > > > > ----- Original Message ----- > > > From: "Mike Kienenberger" <[EMAIL PROTECTED]> > > > To: "Velocity Users List" <velocity-user@jakarta.apache.org> > > > Sent: Monday, October 03, 2005 12:14 PM > > > Subject: Clean way to iterate over a distinct set of items? > > > > > > > > > I'm looking for a clean way to iterate over a distinct set of items. > > > > > > As an example. consider > > > > > > #foreach $employee in $employeeList > > > > > > where $employee.getDepartment() may or may not return a unique value. > > > > > > What I really want to iterate over is the unique set of departments in > > > the employee list. > > > > > > Ideas? > > > > > > And this isn't really what I'm trying to do -- the real example is > > > much more complicated, and there's no easy way to add a tool to the > > > context so that I could have the equivalent of > > > $tool.getUniqueDepartmentsForEmployeeList($employeeList). > > > > > > -Mike > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]