Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie:Differences between arrays

0 views
Skip to first unread message

graham

unread,
Aug 19, 2005, 12:50:03 PM8/19/05
to
Quick question - I have 2 arrays, one which holds the list of people who
had access to a system when I last checked (OLD), and and another which
is the current list of people who should have access (NEW).

Is there some neat syntax in Ruby which would allow me to find the
people added in NEW who are not in OLD, and then find the people in OLD
who have need to be delete as they are not in NEW?

I was going to sort the arrays and iterate through them looking for
differences, but I suspect there is a really cool Ruby syntax to achieve
this.. what is it?
Thx
Graham

Berger, Daniel

unread,
Aug 19, 2005, 1:08:27 PM8/19/05
to
> -----Original Message-----
> From: Derek Wyatt [mailto:de...@derekwyatt.org]
> Sent: Friday, August 19, 2005 10:57 AM
> To: ruby-talk ML
> Subject: Re: Newbie:Differences between arrays
>
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> You want:
>
> ~ added_users = NEW - OLD
>
> Regs,
> D

For set operations you'll probably want to use the "set" package. Part
of the stdlib.

See ruby-talk:65361 for potential gotchas wrt Array diff vs Set diff.

Regards,

Dan


rcoder

unread,
Aug 19, 2005, 1:57:28 PM8/19/05
to
You probably want something like this:

---
require 'set'

OLD = Set.new(%w{ someone admin blah })
NEW = Set.new(%w{ admin blah newacct })

(OLD - NEW).each { |user|
# these users were removed...
}

(NEW - OLD).each { |user|
# and these have been added...
}
---

-Lennon

graham

unread,
Aug 19, 2005, 6:16:42 PM8/19/05
to
rcoder wrote:
> You probably want something like this:
>
> ---
> require 'set'
>
> OLD = Set.new(%w{ someone admin blah })
> NEW = Set.new(%w{ admin blah newacct })
.....
I knew Ruby would have an easy answer. Thanks for the insight.
Graham

William James

unread,
Aug 19, 2005, 9:12:08 PM8/19/05
to
graham wrote:
> Quick question - I have 2 arrays, one which holds the list of people who
> had access to a system when I last checked (OLD), and and another which
> is the current list of people who should have access (NEW).
>
> Is there some neat syntax in Ruby which would allow me to find the
> people added in NEW who are not in OLD, and then find the people in OLD
> who have need to be delete as they are not in NEW?

irb(main):005:0> %w(oldb newa newb) - %w(oldc oldb olda)
=> ["newa", "newb"]

irb(main):006:0> %w(oldc oldb olda) - %w(oldb newa newb)
=> ["oldc", "olda"]

0 new messages