Robert Klemme
3/3/2007 9:54:00 AM
On 03.03.2007 09:52, Yannick Grams wrote:
> Harry wrote:
>>> This is a rather strange request, but here goes. I'm writing a program which
>>> will store output in a .txt file. There are seventeen (17) lines of output,
>>> and each one must be placed under one another. However, when the output gets
>>> too long (about 10200 characters), it messes up the formatting. My question
>>> is whether or not there is a way to make it so that the lines will go on
>>> infinitely. If I haven't explained this very well, forgive me, and let me
>>> know.
>>>
>> Can you show some code?
>> It will make it easier to understand what you mean and where the problem
>> is.
>>
>> Harry
>
> First, a small explanation. Some of you may have heard of ASCII art. It
> involves using symbols such as @ and # in Notepad to create words or
> images. My program will take input from the user, and then turn this
> into ASCII. Here is an excerpt:
>
> line1 = []
> line2 = []
> line3 = []
> line4 = []
> line5 = []
> line6 = []
> line7 = []
> line8 = []
> line9 = []
> line10 = []
> line11 = []
> line12 = []
> line13 = []
> line14 = []
> line15 = []
> line16 = []
> line17 = []
> a1 = "...@@@...."
> a2 = ".@@@@@@@.."
> a3 = "@@@...@@@."
> a4 = "@@@...@@@."
> a5 = "@@@...@@@."
> a6 = "@@@...@@@."
> a7 = "@@@...@@@."
> a8 = "@@@...@@@."
> a9 = "@@@...@@@."
> a10 = "@@@...@@@."
> a11 = "@@@@@@@@@."
> a12 = "@@@@@@@@@."
> a13 = "@@@...@@@."
> a14 = "@@@...@@@."
> a15 = "@@@...@@@."
> a16 = "@@@...@@@."
> a17 = "@@@...@@@."
> puts "INPUT TEST"
> input = gets.chomp.to_s.downcase
> input.each_byte do |x|
> x = x.chr
> if x == "a"
> line1.push(a1)
> line2.push(a2)
> line3.push(a3)
> line4.push(a4)
> line5.push(a5)
> line6.push(a6)
> line7.push(a7)
> line8.push(a8)
> line9.push(a9)
> line10.push(a10)
> line11.push(a11)
> line12.push(a12)
> line13.push(a13)
> line14.push(a14)
> line15.push(a15)
> line16.push(a16)
> line17.push(a17)
> else
> #other letters
> end
> end
> $File = File.open("test.txt", "w")
> $File.puts(line1.join, line2.join, line3.join, line4.join, line5.join,
> line6.join, line7.join, line8.join, line9.join, line10.join,
> line11.join, line12.join, line13.join, line14.join, line15.join,
> line16.join, line17.join)
> $File.close
>
> I know it isn't very good, as I'm only a beginner programmer. This
> excerpt will work for the letter "a" alone. Try it out: it will only
> store a maximum of 102 A's until the format goes all crazy in the
> Notepad file. If you know of any way to fix this, I'd be grateful for
> your help.
** SPOILER **
Don't read on if you want to try further for yourself.
Some suggestions for improvement: use arrays and indexing to hold your
lines and also your character items. Use the block form of File.open as
it is safer.
# const definitions
CHARACTERS = {
?a = [
"...@@@....",
".@@@@@@@..",
# etc.
],
?b = [
],
# etc.
}
# get input
input = "aa"
# create output
lines = Array.new(17) { "" }
input.each_byte do |b|
char = CHARACTERS[b]
lines.each_with_index do |line, idx|
line << char[idx]
end
end
# write output
File.open("out.txt", "w") do |io|
lines.each do |line|
io.puts line
end
end
Even more efficient would be to proceed line wise and not create the
result in memory at all but write directly to the file.
Kind regards
robert