# Appending to an array

**URL:** <https://rubytalk.org/t/appending-to-an-array/9295>\
**Category:** ruby-talk\
**Created:** [11 December 2003 22:46 UTC](https://rubytalk.org/t/appending-to-an-array/9295 "2003-12-11T22:46:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Adam2](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Thomas\_Adam2](https://rubytalk.org/u/Thomas_Adam2)\
**Post date:** [11 December 2003 22:46 UTC](https://rubytalk.org/t/appending-to-an-array/9295/1 "2003-12-11T22:46:47Z")

</div>

Hi All,

I am having a rather weird problem with array manipulation.

I have a file with a load of text in it, and I have a line which looks  
like:

vthread \<\< File.new(pathname,  
“r”).readlines().to\_s().grep(/^foobar/).uniq()

the expression /^foobar/ is repeated throughout, but there are  
differences. Now, I would assume there to be a new entry added to the  
array each time grep returns a match. However, while the code does what I  
want it to, doing a:

vthread.length()

returns “1”

Printing this single element to the screen does indeed yield the results,  
but what I want it to do is to add the match each time into a new element  
in the array.

Am I doing something silly?

Thank You All,

Thomas Adam

> **···**
>
> =====  
> “The Linux Weekend Mechanic” – [http://linuxgazette.net](http://linuxgazette.net)  
> "TAG Editor" – [http://linuxgazette.net](http://linuxgazette.net)
> 
> * * *
> 
> BT Yahoo! Broadband - Save £80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. [http://uk.rd.yahoo.com/evt=21064/\*http://btyahoo.yahoo.co.uk](http://uk.rd.yahoo.com/evt=21064/*http://btyahoo.yahoo.co.uk)

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck4](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jamis\_Buck4](https://rubytalk.org/u/Jamis_Buck4)\
**Post date:** [11 December 2003 22:55 UTC](https://rubytalk.org/t/appending-to-an-array/9295/2 "2003-12-11T22:55:15Z")

</div>

Thomas Adam wrote:

> vthread \<\< File.new(pathname,  
> “r”).readlines().to\_s().grep(/^foobar/).uniq()

The \<\< operator for array only appends the single object given to the  
end of the array–it does not concatenate one array onto another. What  
you probably want is the += operator:

vthread += File.new(…).grep(/^foobar/).uniq()

(Assuming, of course, that vthread is an array.)

> **···**
>
> –  
> Jamis Buck  
> jgb3@email.byu.edu
> 
> ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S\*)|le.l(…)e|#!(\S\*)/) {|r| a \<\< r.compact.first };puts “\n\>#{a.join(%q/ /)}\<\n\n”’

---

<div class="post-metadata">

**Author:** ![Gennady](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@Gennady](https://rubytalk.org/u/Gennady)\
**Post date:** [11 December 2003 23:05 UTC](https://rubytalk.org/t/appending-to-an-array/9295/3 "2003-12-11T23:05:46Z")

</div>

> Hi All,
> 
> I am having a rather weird problem with array manipulation.
> 
> I have a file with a load of text in it, and I have a line which looks  
> like:
> 
> vthread \<\< File.new(pathname,  
> “r”).readlines().to\_s().grep(/^foobar/).uniq()

Here uniq() returns an array and you are adding this single object of  
class Array into vthread. You may want to use

vthread.concat File.new …

instead.

By the way, your example leaves an opened file behind until it is  
garbage collected. While possibly being OK in your environment, in  
general it is better not to leave opened files when you can easily have  
them closed. You can use:

IO.readlines(pathname) instead of File.new(pathname,“r”).readlines or  
do this

vthread.concat File.open(pathname,“r”) { |\_io|  
\_io.readlines().to\_s().grep(/^foobar/).uniq()  
}

When block is done, file will be closed.

> the expression /^foobar/ is repeated throughout, but there are  
> differences. Now, I would assume there to be a new entry added to the  
> array each time grep returns a match. However, while the code does  
> what I  
> want it to, doing a:
> 
> vthread.length()
> 
> returns “1”
> 
> Printing this single element to the screen does indeed yield the  
> results,  
> but what I want it to do is to add the match each time into a new  
> element  
> in the array.
> 
> Am I doing something silly?
> 
> Thank You All,
> 
> Thomas Adam
> 
> =====  
> “The Linux Weekend Mechanic” – [http://linuxgazette.net](http://linuxgazette.net)  
> “TAG Editor” – [http://linuxgazette.net](http://linuxgazette.net)
> 
> * * *
> 
> \_  
> BT Yahoo! Broadband - Save £80 when you order online today. Hurry!  
> Offer ends 21st December 2003. The way the internet was meant to be.  
> [http://uk.rd.yahoo.com/evt=21064/\*http://btyahoo.yahoo.co.uk](http://uk.rd.yahoo.com/evt=21064/*http://btyahoo.yahoo.co.uk)

Sincerely,  
Gennady Bystritsky

> **···**
>
> On Dec 11, 2003, at 14:46, Thomas Adam wrote:

---

<div class="post-metadata">

**Author:** ![Thomas\_Adam2](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Thomas\_Adam2](https://rubytalk.org/u/Thomas_Adam2)\
**Post date:** [11 December 2003 22:59 UTC](https://rubytalk.org/t/appending-to-an-array/9295/4 "2003-12-11T22:59:46Z")

</div>

— Jamis Buck [jgb3@email.byu.edu](mailto:jgb3@email.byu.edu) wrote:

> Thomas Adam wrote:
> 
> > vthread \<\< File.new(pathname,  
> > “r”).readlines().to\_s().grep(/^foobar/).uniq()
> 
> The \<\< operator for array only appends the single object given to the  
> end of the array–it does not concatenate one array onto another. What  
> you probably want is the += operator:
> 
> vthread += File.new(…).grep(/^foobar/).uniq()
> 
> (Assuming, of course, that vthread is an array.)

James,

Many thanks for that!! True genius. Solved it completely. I wonder why I  
never thought of it myself 🙂

kudos\_Points++;

🙂

– Thomas Adam

> **···**
>
> =====  
> “The Linux Weekend Mechanic” – [http://linuxgazette.net](http://linuxgazette.net)  
> “TAG Editor” – [http://linuxgazette.net](http://linuxgazette.net)
> 
> * * *
> 
> BT Yahoo! Broadband - Save £80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. [http://uk.rd.yahoo.com/evt=21064/\*http://btyahoo.yahoo.co.uk](http://uk.rd.yahoo.com/evt=21064/*http://btyahoo.yahoo.co.uk)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [12 December 2003 14:52 UTC](https://rubytalk.org/t/appending-to-an-array/9295/5 "2003-12-12T14:52:00Z")

</div>

“Jamis Buck” [jgb3@email.byu.edu](mailto:jgb3@email.byu.edu) schrieb im Newsbeitrag  
news:3FD8F5A5.7040607@email.byu.edu…

> Thomas Adam wrote:
> 
> > vthread \<\< File.new(pathname,  
> > “r”).readlines().to\_s().grep(/^foobar/).uniq()
> 
> The \<\< operator for array only appends the single object given to the  
> end of the array–it does not concatenate one array onto another. What  
> you probably want is the += operator:
> 
> vthread += File.new(…).grep(/^foobar/).uniq()

Not good: this creates new arrays all the time. Also: File.readlines is  
better since it closes the file automatically:

vthread.push \*File.readlines(pathname).grep(/^foobar/).uniq()  
or  
vthread.concat File.readlines(pathname).grep(/^foobar/).uniq()

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Derek\_Lewis](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Derek\_Lewis](https://rubytalk.org/u/Derek_Lewis)\
**Post date:** [11 December 2003 23:01 UTC](https://rubytalk.org/t/appending-to-an-array/9295/6 "2003-12-11T23:01:34Z")

</div>

The “p” method is awefully handy:

a =   
b =   
c = [1,2,3]  
a \<\< c  
b += c  
p a # =\> [[1, 2, 3]]  
p b # =\> [1, 2, 3]

“puts a” and “puts b” both display the same thing:  
1  
2  
3

Not very helpful 🙂

> **···**
>
> On Fri, 12 Dec 2003, Thomas Adam wrote:
> 
> > — Jamis Buck [jgb3@email.byu.edu](mailto:jgb3@email.byu.edu) wrote:
> > 
> > > Thomas Adam wrote:
> > > 
> > > > vthread \<\< File.new(pathname,  
> > > > “r”).readlines().to\_s().grep(/^foobar/).uniq()
> > > 
> > > The \<\< operator for array only appends the single object given to the  
> > > end of the array–it does not concatenate one array onto another. What  
> > > you probably want is the += operator:
> > > 
> > > vthread += File.new(…).grep(/^foobar/).uniq()
> > > 
> > > (Assuming, of course, that vthread is an array.)
> > 
> > James,
> > 
> > Many thanks for that!! True genius. Solved it completely. I wonder why I  
> > never thought of it myself 🙂
> > 
> > kudos\_Points++;
> > 
> > 🙂
> > 
> > – Thomas Adam
> > 
> > =====  
> > “The Linux Weekend Mechanic” – [http://linuxgazette.net](http://linuxgazette.net)  
> > “TAG Editor” – [http://linuxgazette.net](http://linuxgazette.net)
> > 
> > * * *
> > 
> > BT Yahoo! Broadband - Save £80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. [http://uk.rd.yahoo.com/evt=21064/\*http://btyahoo.yahoo.co.uk](http://uk.rd.yahoo.com/evt=21064/*http://btyahoo.yahoo.co.uk)
> 
> Derek Lewis
> 
> ===================================================================  
> Java Web-Application Developer
> 
> ```
> Email : email@lewisd.com
> Cellular : 604.312.2846
> Website : http://www.lewisd.com
> 
> ```
> 
> “If you’ve got a 5000-line JSP page that has “all in one” support  
> for three input forms and four follow-up screens, all controlled  
> by “if” statements in scriptlets, well … please don’t show it  
> to me :-). Its almost dinner time, and I don’t want to lose my  
> appetite :-).”  
> - Craig R. McClanahan
