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

--
Posted via http://www.ruby-forum.com/.

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.
On Dec 7, 2012 8:04 AM, "Joao Silva" <lists@ruby-forum.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

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|
      results[row] << char
    end
  end

end

p results

--output:--
["NAMEONE ", "NAMETWO ", "NAMETHREE", "NAMEFOUR ", "NAMEFIVE ",
"NAMESIX ", "NAMESEVEN", "NAMEEIGTH"]

···

--
Posted via http://www.ruby-forum.com/.

Joao Silva 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|
      next if lines[row][column] == " "
      string << lines[row][column]
    end

    results.push string
  end

end

p results

--output:--
["NAMEONE", "NAMETWO", "NAMETHREE", "NAMEFOUR", "NAMEFIVE", "NAMESIX",
"NAMESEVEN", "NAMEEIGTH"]

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Fri, Dec 7, 2012 at 4:24 AM, 7stud -- <lists@ruby-forum.com> wrote:

Joao Silva wrote in post #1088123:

results =
rows = 9
columns = 8

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/