Haha, I'm new to Ruby as well. :P If you're only going to have 5 columns in each row then I believe that you're good to go. Though I've come up with the following example that will accomodate for an infinite amount of columns, I'm sure someone will be able to improve upon it.
Test.txt:
Code:
R1E1 R1E2 R1E3 R1E4 R1E5
R2E1 R2E2 R2E3 R2E4 R2E5
R3E1 R3E2 R3E3 R3E4 R3E5
R4E1 R4E2 R4E3 R4E4 R4E5
R5E1 R5E2 R5E3 R5E4 R5E5
Test.rb:
Code:
test_file = "C:\\Documents and Settings\\David Sissitka\\Desktop\\Test.txt"
lines = IO.readlines(test_file)
lines.each_with_index do |line, i|
current_row = line.split("\t")
puts "Row: #{i}"
entry_2 = current_row[1]
entry_3 = current_row[2]
current_row [1] = entry_3
current_row [2] = entry_2
current_row.each_with_index do |column, i|
puts "Column #{i}: #{column}"
end
end
Sample Output:
Code:
Row: 0
Column 0: R1E1
Column 1: R1E3
Column 2: R1E2
Column 3: R1E4
Column 4: R1E5
Column 5:
Row: 1
Column 0: R2E1
Column 1: R2E3
Column 2: R2E2
Column 3: R2E4
Column 4: R2E5
Column 5:
Row: 2
Column 0: R3E1
Column 1: R3E3
Column 2: R3E2
Column 3: R3E4
Column 4: R3E5
Column 5:
Row: 3
Column 0: R4E1
Column 1: R4E3
Column 2: R4E2
Column 3: R4E4
Column 4: R4E5
Column 5:
Row: 4
Column 0: R5E1
Column 1: R5E3
Column 2: R5E2
Column 3: R5E4
Column 4: R5E5
Results are in the form of RXEY where is is the current row and y is the current column.
Bookmarks