Mark Priatel | 10 May 01:35

Re: [groovy-user] grep usage

So you have a list of items (java beans can be accessed the same way as maps)...

def items = [ [id:1,name:'aaa'] , [id:2,name:'bbb'], [id:3,name:'ccc' ] ]

items.grep{ it.id ==1 || it.id == 2 }.each{ println it.name }

prints:
aaa
bbb

'it' is the default name of the current element in the closure's
iteration.  If you want to override this you can do:

items.grep{ p ->  p.id == 1 || p.id ==2 }

-mark

On Fri, May 9, 2008 at 3:06 PM, Vinny <xaymaca@...> wrote:
> Ok, Newb question.
> Is it possible to use the groovy grep method to filter a javabean from a
> list based on the value of
> one of it's properties?
>
> Something on the order of :
>
> for(p in list.grep(p.ID == 111)) {
>      print p
>
> }
>
> // The above code does not work btw.
>
> Thanks in advance
>
>
>
> --
> The future is here. It's just not widely distributed yet.
> -William Gibson

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane