# Array difference

**URL:** <https://rubytalk.org/t/array-difference/38871>\
**Category:** ruby-talk\
**Created:** [5 July 2007 19:20 UTC](https://rubytalk.org/t/array-difference/38871 "2007-07-05T19:20:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Bowen](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jeff\_Bowen](https://rubytalk.org/u/Jeff_Bowen)\
**Post date:** [5 July 2007 19:20 UTC](https://rubytalk.org/t/array-difference/38871/1 "2007-07-05T19:20:17Z")

</div>

Im trying to create an array containing the  
difference of the two other arrays. When I create the  
arrays manual I have no problems.

a = ["0001", "0002"]  
b = ["0001", "0002", "0003"]  
c = b - a  
puts c  
c -\> 0003

However when the array is created from data in a file  
d will equal the first array not the difference  
between the two.

File.open("datafilelist.txt").each { |h| data\_files1  
\<\< h}

d = data\_files1 - data\_files  
puts d  
-\> 0001, 0002

Thoughts?

Jeff Bowen

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Luggage? GPS? Comic books?  
> Check out fitting gifts for grads at Yahoo! Search  
> [http://search.yahoo.com/search?fr=oni\_on\_mail&p=graduation+gifts&cs=bz](http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz)

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [5 July 2007 19:46 UTC](https://rubytalk.org/t/array-difference/38871/2 "2007-07-05T19:46:31Z")

</div>

I guess the trailing newline in the elements of data\_files1 is the culprit. In that case push h.chomp instead of h. In any case, inspect the arrays.

-- fxn

> **···**
>
> On Jul 5, 2007, at 9:20 PM, Jeffrey Bowen wrote:
> 
> > I’m trying to create an array containing the  
> > difference of the two other arrays. When I create the  
> > arrays manual I have no problems.
> > 
> > a = ["0001", "0002"]  
> > b = ["0001", "0002", "0003"]  
> > c = b - a  
> > puts c  
> > c -\> 0003
> > 
> > However when the array is created from data in a file  
> > d will equal the first array not the difference  
> > between the two.
> > 
> > File.open("datafilelist.txt").each { |h| data\_files1  
> > \<\< h}
> > 
> > d = data\_files1 - data\_files  
> > puts d  
> > -\> 0001, 0002

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [5 July 2007 19:53 UTC](https://rubytalk.org/t/array-difference/38871/3 "2007-07-05T19:53:47Z")

</div>

You haven't shown enough code to get any help! What is data\_files? Giving us a sample of the contents of "datafilelist.txt" would be quite useful, too.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com  
k

> **···**
>
> On Jul 5, 2007, at 3:20 PM, Jeffrey Bowen wrote:
> 
> > I’m trying to create an array containing the  
> > difference of the two other arrays. When I create the  
> > arrays manual I have no problems.
> > 
> > a = ["0001", "0002"]  
> > b = ["0001", "0002", "0003"]  
> > c = b - a  
> > puts c  
> > c -\> 0003
> > 
> > However when the array is created from data in a file  
> > d will equal the first array not the difference  
> > between the two.
> > 
> > File.open("datafilelist.txt").each { |h| data\_files1  
> > \<\< h}
> > 
> > d = data\_files1 - data\_files  
> > puts d  
> > -\> 0001, 0002
> > 
> > Thoughts?
> > 
> > Jeff Bowen

---

<div class="post-metadata">

**Author:** ![Jeff\_Bowen](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jeff\_Bowen](https://rubytalk.org/u/Jeff_Bowen)\
**Post date:** [5 July 2007 19:52 UTC](https://rubytalk.org/t/array-difference/38871/4 "2007-07-05T19:52:29Z")

</div>

Thanks. inspect showed the problem.

> **···**
>
> --- Xavier Noria \<fxn@hashref.com\> wrote:
> 
> > On Jul 5, 2007, at 9:20 PM, Jeffrey Bowen wrote:
> > 
> > \> Im trying to create an array containing the  
> > \> difference of the two other arrays. When I create  
> > the  
> > \> arrays manual I have no problems.  
> > \>  
> > \> a = ["0001", "0002"]  
> > \> b = ["0001", "0002", "0003"]  
> > \> c = b - a  
> > \> puts c  
> > \> c -\> 0003  
> > \>  
> > \> However when the array is created from data in a  
> > file  
> > \> d will equal the first array not the difference  
> > \> between the two.  
> > \>  
> > \> File.open("datafilelist.txt").each { |h|  
> > data\_files1  
> > \> \<\< h}  
> > \>  
> > \> d = data\_files1 - data\_files  
> > \> puts d  
> > \> -\> 0001, 0002
> > 
> > I guess the trailing newline in the elements of  
> > data\_files1 is the  
> > culprit. In that case push h.chomp instead of h. In  
> > any case, inspect  
> > the arrays.
> > 
> > -- fxn
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Sick sense of humor? Visit Yahoo! TV's  
> Comedy with an Edge to see what's on, when.  
> [http://tv.yahoo.com/collections/222](http://tv.yahoo.com/collections/222)
