Simple text parsing

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

"Remington Splettstoesser" <remi500@freenet.de> 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

Looks like you have a specific field separator, so might as well use it:

    print_format = "First Name: %s\nLast Name: %s\nCity: %s\n\n"

    File.foreach("test.txt") do |line|
      first, last, city = line.chomp.split(/ \| /)
      print print_format % [first, last, city]
    end

···

At 2010-04-08 10:44AM, "Remington Splettstoesser" wrote:

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

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Watch out, both of these solutions will fail for

John | O'Brian | New York

Thank You gfb!

Remington

···

On Apr 8, 2010, at 5:55 PM, GianFranco Bozzetti wrote:

"Remington Splettstoesser" <remi500@freenet.de> 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