Copying values from a different file into variables

I would like to copy specific lines(contents) from file1 into file2.

File1 has following information:

browser = “firefox”
id = “[email protected]
password= “12345678”
account = “Yes”

I want the following information in File2 :

@var1 = “firefox” (value of browser from File1)
@var2 = “[email protected]” (value of id from File1)
@var3 = “12345678” (value of password from File1)

How do i get this done using ruby. Please let me know if you require
more clarification on this front.

Cheers

On Aug 18, 2009, at 10:23 AM, Shekar Ls wrote:

I would like to copy specific lines(contents) from file1 into file2.

You’d generally get more help if you try to do some of the work and
then post if/when you get stuck.

@var1 = “firefox” (value of browser from File1)
@var2 = “[email protected]” (value of id from File1)
@var3 = “12345678” (value of password from File1)

How do i get this done using ruby. Please let me know if you require
more clarification on this front.

I haven’t used ruby that long but something like

File.open(“file2”, “w”) do |f2|
count = 1
File.foreach(“file1”) do |line|
f2 << line.sub(/^[\w\s]+=/, “@var#{ count } =”)
count += 1
end
end

could work. (skipping the creation of @var4 for the “account” line is
left as an exercise for the OP)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs