# Array difference

**URL:** <https://www.ruby-forum.com/t/array-difference/108666>\
**Category:** Ruby\
**Created:** [July 5, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/array-difference/108666 "2007-07-05T21:21:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [July 5, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/array-difference/108666/1 "2007-07-05T21:21:08Z")

</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 B.

```
  ____________________________________________________________________________________

```

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:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [July 5, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/array-difference/108666/2 "2007-07-05T21:48:13Z")

</div>

On Jul 5, 2007, at 9:20 PM, Jeffrey B. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [July 5, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/array-difference/108666/3 "2007-07-05T21:54:14Z")

</div>

Thanks. inspect showed the problem.

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [July 5, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/array-difference/108666/4 "2007-07-05T21:55:14Z")

</div>

On Jul 5, 2007, at 3:20 PM, Jeffrey B. wrote:

> However when the array is created from data in a file  
> Thoughts?
> 
> Jeff B.

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
k
