Read text file and set the values in the Variables

  • Thread starter KingMaker KingMaker
  • Start date
K

KingMaker KingMaker

Text file:
firstName=abcd
lastName=xyz
legalName=test
vendorRepWorkPhone=121212
[email protected]
[email protected]

First i read the file and get all values in the data variable.


contents = File.read("c:/test/Flow1.txt")
data = contents.map do |line|
line.chomp.split("=")[1]
end



than put values in the variable like.
@FirstName=
@LastName=
@LegalName=
@VendorRepWorkPhone=
@Email=data
@ConFirmEmail=



i use array for copy the values like
data[0] for first line
data[1] for second line

but it is not good if i have 100 value than i write this for 100 times
in the feature requirement is changed and add some field that time.
problem arise so plz help me..
what is the best solution of this....
 
M

Marcin Wolski

KingMaker said:
Text file:
firstName=abcd
lastName=xyz
legalName=test
vendorRepWorkPhone=121212
[email protected]
[email protected]

First i read the file and get all values in the data variable.


contents = File.read("c:/test/Flow1.txt")
data = contents.map do |line|
line.chomp.split("=")[1]
end



than put values in the variable like.
@FirstName=
@LastName=
@LegalName=
@VendorRepWorkPhone=
@Email=data
@ConFirmEmail=



i use array for copy the values like
data[0] for first line
data[1] for second line

but it is not good if i have 100 value than i write this for 100 times
in the feature requirement is changed and add some field that time.
problem arise so plz help me..
what is the best solution of this....

And what about storing that data in a hash, e.g:

data = Hash.new
File.readlines("c:/test/Flow1.txt").each do |line|
var,val = line.chomp.split("=")
data[var] = val
end

puts data['lastName']
puts data['vendorRepWorkPhone']
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top