From: Matthew Kerwin Date: 2012-12-07T08:28:54+09:00 Subject: Re: Rows and columns in a text file. --e89a8ff1ca524ba96304d0377662 Content-Type: text/plain; charset=ISO-8859-1 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" 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/. > --e89a8ff1ca524ba96304d0377662 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

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" &= lt;lists@ruby-forum.com> wro= te:
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
=A0=A0ERE ET
=A0=A0E =A0 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 =3D File.new ("names1.txt", "r")
#j =3D File.new ("names2.txt", "w+")
a =3D []
b =3D []

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

rows =3D 7
columns =3D 8

for i in 0..rows-1
=A0=A0 for j in 0..columns-1
=A0=A0 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/.
--e89a8ff1ca524ba96304d0377662--