Rows and columns in a text file

Hi all.

I’m doing an exercise where given a text file with words written in
columns or vertically, I have to put in another file, but horizontally.

For example, I have the file:

NNNNNNNN
AAAAAAAA
MMMMMMMM
EEEEEEEE
OTTFFSSE
NWHOIIEI
EORUVXVG
ERE ET
E NH

I seek to place their names in another file like this:

NAMEONE
NAMETWO
NAMETHREE
NAMEFOUR
NAMEFIVE
NAMESIX
NAMESEVEN
NAMEEIGHT

This is the code you were doing (in this case I use an array to test if
it worked):

#########################################################################

f = File.new (“names1.txt”, “r”)
#j = File.new (“names2.txt”, “w+”)
a = []
b = []

f.each do |line|
a.push(line)
end
f.close

rows = 7
columns = 8

for i in 0…rows-1
for j in 0…columns-1
b.push(a[i][j])
end

puts b

########################################################################

The issue is that I find the way the words are written horizontally, if
I try to access their values ​​one by one, it seems that the place
properly, but if I use a cycle does not work.

Thanks

I imagine there’s something you could do like:

f.each.map{|line|line.scan /./ }.transpose.map{|col|col.join}

But I just made that up.

Sent from my phone, so excuse the typos.

results = nil

File.open (“data.txt”) do |f|
lines = f.readlines
results = Array.new(lines[0].length - 1) { “” } #–> ["", “”, …]

lines.each do |line|
line = line.chomp

line.each_char.with_index do |char, row|
  next if char == " "
  results[row] << char
end

end

end #File is closed automatically here

–output:–
[“NAMEONE”, “NAMETWO”, “NAMETHREE”, “NAMEFOUR”, “NAMEFIVE”, “NAMESIX”,
“NAMESEVEN”, “NAMEEIGTH”]

Joao S. wrote in post #1088123:

f = File.new (“names1.txt”, “r”)
#j = File.new (“names2.txt”, “w+”)
a = []
b = []

f.each do |line|
a.push(line)
end
f.close

rows = 7
columns = 8

for i in 0…rows-1
for j in 0…columns-1
b.push(a[i][j])
end

puts b

########################################################################

The issue is that I find the way the words are written horizontally, if
I try to access their values ​​one by one, it seems that the place
properly, but if I use a cycle does not work.

Thanks

results = []
rows = 9
columns = 8

File.open (“data.txt”) do |f|
lines = f.readlines

(0…columns).each do |column|
string = “”

(0...rows).each do |row|
  current_char = lines[row][column]
  next if current_char == " "
  string << current_char
end

results.push string

end

end

–output:–
[“NAMEONE”, “NAMETWO”, “NAMETHREE”, “NAMEFOUR”, “NAMEFIVE”, “NAMESIX”,
“NAMESEVEN”, “NAMEEIGTH”]

On Fri, Dec 7, 2012 at 4:24 AM, 7stud – [email protected] wrote:

Joao S. wrote in post #1088123:

results = []
rows = 9
columns = 8

Hey, that’s cheating. :slight_smile: The algorithm must determine these from the
input.

Two more solutions

puts “-- 1 --”

lines = []
max = 0

File.foreach “t” do |line|
line.chomp!
lines << line
max = [max, line.length].max
end

max.times do |i|
puts lines.map {|l| l[i] || ’ '}.join
end

puts “-- 2 --”

lines = []

File.foreach “t” do |line|
line.chomp!
line.each_char.each_with_index do |c, i|
(lines[i] ||= ‘’) << c
end
end

puts lines

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs