I have a text file that I want to add some content to, but I'm not sure
how to use the file as input. I have a list of things in the file that
is formatted like
item1 | item2 | item3 | item4 |
and etcetera... How can I take these items and put each of them on a new
line, minus the "|" and add a certain phrase afterward? Like:
item1 [phrase]
item2 [phrase]
item3 [phrase]
Is there a simple way to do this? I've taken lists of things like this
before and added text to it, but I put all of the items in an array
manually first... would someone show me a faster way, please?
File.open("yourfile_o.txt","w") do |file|
file.write File.open("yourfile.txt").readlines.join("\n").gsub(/([^\|]+)\|/){
"#{(v=$1.strip) + " " + ph_hashes[v.to_sym]}\n"}
end
···
On Tue, May 6, 2008 at 8:34 AM, Zoe Phoenix <dark_sgtphoenix@hotmail.com> wrote:
I have a text file that I want to add some content to, but I'm not sure
how to use the file as input. I have a list of things in the file that
is formatted like
item1 | item2 | item3 | item4 |
and etcetera... How can I take these items and put each of them on a new
line, minus the "|" and add a certain phrase afterward? Like:
item1 [phrase]
item2 [phrase]
item3 [phrase]
Is there a simple way to do this? I've taken lists of things like this
before and added text to it, but I put all of the items in an array
manually first... would someone show me a faster way, please?
--
Posted via http://www.ruby-forum.com/\.
I have a text file that I want to add some content to, but I'm not sure
how to use the file as input. I have a list of things in the file that
is formatted like
item1 | item2 | item3 | item4 |
and etcetera... How can I take these items and put each of them on a new
line, minus the "|" and add a certain phrase afterward? Like:
item1 [phrase]
item2 [phrase]
item3 [phrase]
Is there a simple way to do this? I've taken lists of things like this
before and added text to it, but I put all of the items in an array
manually first... would someone show me a faster way, please?
Hi Zoe,
Ruby program
···
**********
full= File.open("orig.txt")
phrase=["phrase1","phrase2","phrase3"]
count=0
full.each do |line|
first=
first=line.split(/\|/)
first.each do |single|
sub=single.strip!
main = (sub).to_s + (phrase [count]).to_s
puts main
count+=1
end
end
full= File.open("orig.txt")
phrase=["phrase1","phrase2","phrase3"]
count=0
full.each do |line|
first=
first=line.split(/\|/)
first.each do |single|
sub=single.strip!
main = (sub).to_s + (phrase [count]).to_s
puts main
count+=1
end
end
how can I save the output to a file? I see where you're making it print
the output with "main", but when I try to save it to a file the way I
have with other programs I've made, I get an error... it says the
variable "main" is undefined. Help?