From: Remington Splettstoesser Date: 2010-04-09T05:36:46+09:00 Subject: Re: simple text parsing Thank You gfb! Remington On Apr 8, 2010, at 5:55 PM, GianFranco Bozzetti wrote: > > "Remington Splettstoesser" wrote in message > news:9E14B810-41E6-4002-AB4D-894F60F14687@freenet.de... > Hello Everyone, > > What is the best way to get this input > > test.txt > ------------------------------------------------ > David | Hansson | Chicago > Obie | Fernandez | Jacksonville > ------------------------------------------------ > > to this output: > > First Name: David > Last Name: Hansson > City: Chicago > > First Name: Obie > Last Name: Fernandez > City: Jacksonville > > ????????????????????????? > > This is what I've tried so far.. I'm pretty far off I believe. Any > suggestions would help with learning the ins and outs of Ruby (or > programming in general) > > ----------------------------- > test.rb > ---------------------------- > myArray = [] > file = File.open("test.txt", 'r+') > file.each_line do |line| > x = line.scan(/\w+/).inspect > myArray = x > puts myArray > end > > my output > ----------------------- > $ ruby test.rb > ["David", "Hansson", "Chicago"] > ["Obie", "Fernandez", "Jacksonville"] > > > Thanks in Advance > Remington Splettstoesser > > A solution is to use printf as in the folloving code: > > # test1.rb > file = File.open("test.txt", 'r+') > file.each_line do |line| > myArray = line.scan(/\w+/) > # myArray = line.split(/[\s|]+/) Alternative solution to scan > printf("\nFirst Name: %-s\nLast Name : %-s\nCity : %-s\n", *myArray) > end > > Hth gfb > > > >