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
on 2012-12-06 23:03
on 2012-12-07 00:30
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 2012-12-07 03:48
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"]
on 2012-12-07 04:23
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| 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 2012-12-08 11:54
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 Hey, that's cheating. :-) 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.