Modifying files

Hi all,

I’m trying to modify a file that has tab delimited fields. I want to
modify the 4th field within each row. I can get to the field I want
by using the “split” function operating on \t delimiter… but this
function returns an array right? It doesn’t return a pointer to the
fields?

So if it doesn’t return a pointer then how can I modify the field
within the file easily?

a = File.open(“blah.txt”)
f = (a.gets).split(/\t/)
f[4] = f[4] + “blah”
… what do I do after this? How can I write the value back out to
the file?

Thanks,

  • Khurram

You should be looking to write out the file again with modifications
made, so for example, iterate over each line in the file, change the
ones you wish to, and write the line back out to a new file, or read in
the whole file into an array (see IO.readlines), modify the array, and
then write the array of lines back out to the original file.

Tom.

···

Hi all,

I’m trying to modify a file that has tab delimited fields. I want to
modify the 4th field within each row. I can get to the field I want
by using the “split” function operating on \t delimiter… but this
function returns an array right? It doesn’t return a pointer to the
fields?

So if it doesn’t return a pointer then how can I modify the field
within the file easily?

a = File.open(“blah.txt”)
f = (a.gets).split(/\t/)
f[4] = f[4] + “blah”
… what do I do after this? How can I write the value back out to
the file?


.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------’

f.join("\t")

will re-construct the array into a string.

···

On Friday 02 August 2002 10:14 am, Khurram wrote:

Hi all,

I’m trying to modify a file that has tab delimited fields. I want
to modify the 4th field within each row. I can get to the field I
want by using the “split” function operating on \t delimiter… but
this function returns an array right? It doesn’t return a pointer
to the fields?

So if it doesn’t return a pointer then how can I modify the field
within the file easily?

a = File.open(“blah.txt”)
f = (a.gets).split(/\t/)
f[4] = f[4] + “blah”
… what do I do after this? How can I write the value back out
to the file?


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Hi,

···

At Sat, 3 Aug 2002 02:14:37 +0900, Khurram wrote:

a = File.open(“blah.txt”)
f = (a.gets).split(/\t/)
f[4] = f[4] + “blah”

File.open(“blah.txt.new”, “w”) do |b|
print f.join("\t")
end
File.rename(“blah.txt.new”, “blah.txt”)


Nobu Nakada

Should be b.print f.join("\t") ?
Or b.puts f.join("\t") ?

–Gavin

···

----- Original Message -----
From: nobu.nokada@softhome.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, August 03, 2002 4:18 AM
Subject: Re: Modifying files.

Hi,

At Sat, 3 Aug 2002 02:14:37 +0900, > Khurram wrote:

a = File.open(“blah.txt”)
f = (a.gets).split(/\t/)
f[4] = f[4] + “blah”

File.open(“blah.txt.new”, “w”) do |b|
print f.join("\t")
end
File.rename(“blah.txt.new”, “blah.txt”)


Nobu Nakada

Hi,

···

At Sun, 4 Aug 2002 09:49:05 +0900, Gavin Sinclair wrote:

Should be b.print f.join("\t") ?
Or b.puts f.join("\t") ?

Yes, sorry, it’s untested.


Nobu Nakada