[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

array question

Li Chen

9/15/2008 8:27:00 PM

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

Thanks,

Li
--
Posted via http://www.ruby-....

13 Answers

Nathan Powell

9/15/2008 8:43:00 PM

0

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:
> Hi all,
>
> Just wonder:
>
> 1)When substracting array2 from array1 to get the difference elements
> between two arrays(array1-array2), must array1.size >= array2?

irb is great for this stuff.

>> [1, 2] - [1, 3, 4]
=> [2]

> 2)Is there mentod in Array class that can return the element only
> unique to either of two arrays?

http://ruby-doc.org/core/classes/...

> For example, array1=[1,2,3],array2=[1,4,7]
> the return array is [2,3,4,7]


There might be a better way, but I came up with:

>> ([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])
=> [2, 3, 4, 7]

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
~ Steve Prefontaine
------------------------------------

Rob Biedenharn

9/15/2008 8:54:00 PM

0


On Sep 15, 2008, at 4:42 PM, Nathan Powell wrote:

> On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:
>> Hi all,
>>
>> Just wonder:
>>
>> 1)When substracting array2 from array1 to get the difference elements
>> between two arrays(array1-array2), must array1.size >= array2?
>
> irb is great for this stuff.
>
>>> [1, 2] - [1, 3, 4]
> => [2]
>
>> 2)Is there mentod in Array class that can return the element only
>> unique to either of two arrays?
>
> http://ruby-doc.org/core/classes/...
>
>> For example, array1=[1,2,3],array2=[1,4,7]
>> the return array is [2,3,4,7]
>
>
> There might be a better way, but I came up with:
>
>>> ([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])
> => [2, 3, 4, 7]
>
> --
> nathan
> nathan_at_nathanpowell_dot_org
>
> What kind of crazy nut would spend two or three hours a day just
> running?
> ~ Steve Prefontaine
> ------------------------------------

irb> (a1 | a2) - (a1 & a2)
=> [2, 3, 4, 7]

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Joel VanderWerf

9/15/2008 9:05:00 PM

0

Nathan Powell wrote:
> On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:
...
>> 2)Is there mentod in Array class that can return the element only
>> unique to either of two arrays?
>
> http://ruby-doc.org/core/classes/...
>
>> For example, array1=[1,2,3],array2=[1,4,7]
>> the return array is [2,3,4,7]
>
>
> There might be a better way, but I came up with:
>
>>> ([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])
> => [2, 3, 4, 7]
>

Or:

(a1|a2) - (a1&a2)

Or:

require 'set'
Set.new(a1) ^ Set.new(a2)


(I thought I remem

Joel VanderWerf

9/15/2008 9:26:00 PM

0

Joel VanderWerf wrote:
> require 'set'
> Set.new(a1) ^ Set.new(a2)
>
>
> (I thought I remem
bered that Array had a symmetric difference
operator, but apparently not.)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Siep Korteling

9/15/2008 9:30:00 PM

0

Li Chen wrote:
(...)
> 2)Is there mentod in Array class that can return the element only
> unique to either of two arrays?
>
> For example, array1=[1,2,3],array2=[1,4,7]
> the return array is [2,3,4,7]
>
> Thanks,
>
> Li

It's probably academic, but what is the desired result with these 2
arrays?
array1 = [1,2,2,3]
array2 = [1,4,7,7]

Nathan Powell's method: [2, 2, 3, 4, 7, 7]
Rob Biedenharn's method: [2, 3, 4, 7]
Perhaps desired result: [3,4] ?

Regards,

Siep
--
Posted via http://www.ruby-....

Siep Korteling

9/15/2008 9:40:00 PM

0

Joel VanderWerf wrote:
(...)
>
> Or:
>
> require 'set'
> Set.new(a1) ^ Set.new(a2)
>
I figured this out by typo, allthough it is in the documentation.
Anyway, you can do
Set.new(a1) ^ a2

if a2 is enumerable.

Regards,

Siep
--
Posted via http://www.ruby-....

Amos King

9/16/2008 2:13:00 PM

0

Add the two arrays and call uniq on the new array

On Mon, Sep 15, 2008 at 4:39 PM, Siep Korteling <s.korteling@gmail.com> wrote:
> Joel VanderWerf wrote:
> (...)
>>
>> Or:
>>
>> require 'set'
>> Set.new(a1) ^ Set.new(a2)
>>
> I figured this out by typo, allthough it is in the documentation.
> Anyway, you can do
> Set.new(a1) ^ a2
>
> if a2 is enumerable.
>
> Regards,
>
> Siep
> --
> Posted via http://www.ruby-....
>
>



--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInfor...
--
Looking for something to do? Visit http://I...

Nathan Powell

9/16/2008 2:19:00 PM

0

On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:
> Add the two arrays and call uniq on the new array

>> ([1, 2, 3] + [1, 4, 7]).uniq
=> [1, 2, 3, 4, 7]

That doesn't result in what he asked it to result in. I am not
convinced what I came up with was what he was really after either. I
think Siep was on to something.

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
~ Steve Prefontaine
------------------------------------

Amos King

9/16/2008 2:23:00 PM

0

We'll see.

On Tue, Sep 16, 2008 at 9:18 AM, Nathan Powell <nathan@nathanpowell.org> wrote:
> On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:
>> Add the two arrays and call uniq on the new array
>
>>> ([1, 2, 3] + [1, 4, 7]).uniq
> => [1, 2, 3, 4, 7]
>
> That doesn't result in what he asked it to result in. I am not
> convinced what I came up with was what he was really after either. I
> think Siep was on to something.
>
> --
> nathan
> nathan_at_nathanpowell_dot_org
>
> What kind of crazy nut would spend two or three hours a day just running?
> ~ Steve Prefontaine
> ------------------------------------
>
>



--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInfor...
--
Looking for something to do? Visit http://I...

Amos King

9/16/2008 2:25:00 PM

0

Forgot to ask what he was looking for.

On Tue, Sep 16, 2008 at 9:29 AM, Amos King <amos.l.king@gmail.com> wrote:
> We'll see.
>
> On Tue, Sep 16, 2008 at 9:18 AM, Nathan Powell <nathan@nathanpowell.org> wrote:
>> On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:
>>> Add the two arrays and call uniq on the new array
>>
>>>> ([1, 2, 3] + [1, 4, 7]).uniq
>> => [1, 2, 3, 4, 7]
>>
>> That doesn't result in what he asked it to result in. I am not
>> convinced what I came up with was what he was really after either. I
>> think Siep was on to something.
>>
>> --
>> nathan
>> nathan_at_nathanpowell_dot_org
>>
>> What kind of crazy nut would spend two or three hours a day just running?
>> ~ Steve Prefontaine
>> ------------------------------------
>>
>>
>
>
>
> --
> Amos King
> A. King Software Development and Consulting, L.C.
> http://dirtyInfor...
> --
> Looking for something to do? Visit http://I...
>



--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInfor...
--
Looking for something to do? Visit http://I...