[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array select

Luca Roma

11/12/2007 2:06:00 PM

i have 2 arrays of objects: dates and places

dates have attribute place_id
and places have id

I must to selezionate all places that have id in array of objects dates.

dates.select{|obj| obj.place_id in places[].id }

How i can replace "in places[].id " to works it?
Thanks
--
Posted via http://www.ruby-....

6 Answers

Robert Klemme

11/12/2007 3:09:00 PM

0

2007/11/12, Luca Roma <roma@nerto.it>:
> i have 2 arrays of objects: dates and places
>
> dates have attribute place_id
> and places have id
>
> I must to selezionate all places that have id in array of objects dates.
>
> dates.select{|obj| obj.place_id in places[].id }
>
> How i can replace "in places[].id " to works it?

Create a Set of place ids and test against that when selecting.
Alternative use #any? on places to test for a place with the current
date's place_id but this is very inefficient.

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Max Williams

11/12/2007 3:12:00 PM

0

Luca Roma wrote:
> i have 2 arrays of objects: dates and places
>
> dates have attribute place_id
> and places have id
>
> I must to selezionate all places that have id in array of objects dates.
>
> dates.select{|obj| obj.place_id in places[].id }
>
> How i can replace "in places[].id " to works it?
> Thanks

You could use Array#collect, which creates a new array out of another
array, taking a block of code that processes an element of the old array
and spits out something to go into the new array.

so,
places[].id

would be
places.collect{ |place| place.id}

You can then ask if obj.place_id is in this, as follows:

dates.select{ |obj| places.collect{ |place|
place.id}.include?(obj.place_id) }

I think that's what you're after (i'm not exactly sure from your
question, sorry).
--
Posted via http://www.ruby-....

Max Williams

11/12/2007 3:14:00 PM

0

Max Williams wrote:
> Luca Roma wrote:
>> i have 2 arrays of objects: dates and places
>>
>> dates have attribute place_id
>> and places have id
>>
>> I must to selezionate all places that have id in array of objects dates.
>>
>> dates.select{|obj| obj.place_id in places[].id }
>>
>> How i can replace "in places[].id " to works it?
>> Thanks
>
> You could use Array#collect, which creates a new array out of another
> array, taking a block of code that processes an element of the old array
> and spits out something to go into the new array.
>
> so,
> places[].id
>
> would be
> places.collect{ |place| place.id}
>
> You can then ask if obj.place_id is in this, as follows:
>
> dates.select{ |obj| places.collect{ |place|
> place.id}.include?(obj.place_id) }
>
> I think that's what you're after (i'm not exactly sure from your
> question, sorry).

Just occurred to me that this is horribly inefficient as you remake the
collection for each member of dates! It would be better to make the new
array first:

place_ids = places.collect{ |place| place.id}
dates.select{ |obj| place_ids.include?(obj.place_id) }
--
Posted via http://www.ruby-....

suzeeq

10/29/2013 2:44:00 PM

0

Adam H. Kerman wrote:
> Barry Margolin <barmar@alum.mit.edu> wrote:
>
>> Preempted because of the World Series.
>
>> Don't bother responding in your usual, Seamusy way. I've already plonked
>> the thread.
>
> And 100% of Barry's Usenet participation has become troll feeding, making
> Barry worse than seamus.

Hardly all of his posts.

Adam H. Kerman

10/29/2013 3:34:00 PM

0

suzeeq <suzee@imbris.com> wrote:
>Adam H. Kerman wrote:
>>Barry Margolin <barmar@alum.mit.edu> wrote:

>>>Preempted because of the World Series.

>>>Don't bother responding in your usual, Seamusy way. I've already plonked
>>>the thread.

>>And 100% of Barry's Usenet participation has become troll feeding, making
>>Barry worse than seamus.

>Hardly all of his posts.

I haven't seen non-troll-feeding followups from Barry recently.

suzeeq

10/29/2013 3:50:00 PM

0

Adam H. Kerman wrote:
> suzeeq <suzee@imbris.com> wrote:
>> Adam H. Kerman wrote:
>>> Barry Margolin <barmar@alum.mit.edu> wrote:
>
>>>> Preempted because of the World Series.
>
>>>> Don't bother responding in your usual, Seamusy way. I've already plonked
>>>> the thread.
>
>>> And 100% of Barry's Usenet participation has become troll feeding, making
>>> Barry worse than seamus.
>
>> Hardly all of his posts.
>
> I haven't seen non-troll-feeding followups from Barry recently.

Are you confusing him with Barry Worthington? Whose name is being
spoofed by the troll I think.