Appending to an array

T

Thomas Adam

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
"TAG Editor" -- 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
 
J

Jamis Buck

Thomas said:
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.)
 
G

Gennady

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
"TAG Editor" -- 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

Sincerely,
Gennady Bystritsky
 
R

Robert Klemme

Jamis Buck said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top