copying values from a different file into variables

S

Shekar Ls

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

# File1 has following information:

browser = "firefox"
id = "(e-mail address removed)"
password= "12345678"
account = "Yes"


# I want the following information in File2 :

@var1 = "firefox" (value of browser from File1)
@var2 = "(e-mail address removed)" (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
 
P

Patrick Okui

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.
# File1 has following information:

browser = "firefox"
id = "(e-mail address removed)"
password= "12345678"
account = "Yes"


# I want the following information in File2 :

@var1 = "firefox" (value of browser from File1)
@var2 = "(e-mail address removed)" (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)
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top