Hi again. I am starting a new topic since this question is not the same
as my original topic started this morning. “It builds on to that
question.”
Originally I was reading a textfile that had values that were comma
delimited. The values were not being outputted to separated lines. Now
that they do that…I was hoping that someone could help me to add a
comment before the line goes into splitting. Thanks everyone for being
so helpful! MC
Question:
Do you know how to add a string on the end of each value before
splitting into a new line? For instance…
textfile contents: value1,value2,value3…
value1 comments
value2 comments
value3 comments
I tried this using Robert’s code, however, it put the comments on a
newline.
File.foreach “temp.txt” do |line|
puts “”, line.split(/,/),“comments” + @usercomments
end
Hi again. I am starting a new topic since this question is not the same
as my original topic started this morning. “It builds on to that
question.”
Originally I was reading a textfile that had values that were comma
delimited. The values were not being outputted to separated lines. Now
that they do that…I was hoping that someone could help me to add a
comment before the line goes into splitting. Thanks everyone for being
so helpful! MC
Question:
Do you know how to add a string on the end of each value before
splitting into a new line? For instance…
textfile contents: value1,value2,value3…
value1 comments
value2 comments
value3 comments
I tried this using Robert’s code, however, it put the comments on a
newline.
File.foreach “temp.txt” do |line|
puts “”, line.split(/,/),“comments” + @usercomments
end
Hi Mom,
Collect all data from file and store it as a single String variable @a. So @a.class==String
Ex, @file=“value1,value2,value3,value4”
Code
@file=“value1,value2,value3,value4”
@output=[] @file.split(/,/).each do |record| @output << record + " comments"
end
puts @output # => It gives => [“value1 comments”,“value2
comments”,“value3 comments”,“value4 comments”]