File Question

Hello to all!

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.

Thanks in advance.

···

_________________________________________________________________
Advertisement: Fresh jobs daily. Stop waiting for the newspaper. Search Now! www.seek.com.au http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn.seek.com.au&_t=757263760&_r=Hotmail_EndText_Dec06&_m=EXT

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

···

--

Japanese Ruby List Subjects in English

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.

···

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

That's a limitation of Notepad.

···

On Sat, Mar 03, 2007 at 05:52:46PM +0900, Yannick Grams wrote:

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.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

P.S. Sorry for the double post, but I forgot to mention that in order to
view the Notepad file properly, you need to go to Format > Font... and
set the font to Lucida Console, Regular, Size 1.

···

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

** 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

···

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.

Chad Perrin wrote:

That's a limitation of Notepad.

Do you have any suggestions?

···

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

Thankyou very much for the helpful hints. A quick question: how long does it take until one is able to think of smart ways around programming? I've been doing this for about a year, and I have improved greatly, but not as much as I should like.

···

From: Robert Klemme <shortcutter@googlemail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: File Question
Date: Sat, 3 Mar 2007 18:55:09 +0900

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

_________________________________________________________________
Advertisement: Meet Sexy Singles Today @ Lavalife - Click here http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Flavalife9.ninemsn.com.au%2Fclickthru%2Fclickthru.act%3Fid%3Dninemsn%26context%3Dan99%26locale%3Den_AU%26a%3D23769&_t=754951090&_r=endtext_lavalife_dec_meet&_m=EXT

In my personal experience, SciTE is excellent. Even WordPad doesn't
have that limitation (though last I checked it still has a memory leak
that was present in the Write program from which it evolved, back in the
Windows 3.x days -- not my favorite tool for reading text files).

Others may offer other suggestions.

I haven't checked in depth on what exactly limits Notepad -- whether
it suffers hardcoded limitations or the limits have something to do with
system RAM. I just know that significant filesizes and line lengths
tend to cause it to "puke", as 'twere, when other text editing
applications don't have that problem.

···

On Sat, Mar 03, 2007 at 06:04:00PM +0900, Yannick Grams wrote:

Chad Perrin wrote:
> That's a limitation of Notepad.

Do you have any suggestions?

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Brian K. Reid: "In computer science, we stand on each other's feet."

Um, that's a much more difficult question than the other one. :slight_smile: The general answer to this is probably (as often) "it depends". For me it certainly took a degree in computer science plus several years software development and probably also several programming languages. Others might get the hang of it faster or slower - that probably depends on yourself and your occupation (doing software development full time certainly helps) and what other factors you can think of.

Studying CR certainly helps as this will provide you with some basic concepts and basic understanding (for example, estimating algorithmic complexity) but that brings you only half there. I do not know a proper replacement for experience, as you can learn all the principles in theory but you will be able to apply them properly only with experience. Also, there is a ton of other issues in practice that the university did not teach us at the time I was attending.

The single most important concept in software engineering is IMHO abstraction. By abstraction I mean the way how you distribute functionality across a system (this holds true for large as well small pieces of software). Create artifacts (classes, methods, functions) that do one thing properly. The hard bit is often to determine what this "one thing" is. :slight_smile:

Having said that, you should probably be a bit more patient. :slight_smile:

Kind regards

  robert

···

On 03.03.2007 11:41, Yannick Grams wrote:

Thankyou very much for the helpful hints. A quick question: how long does it take until one is able to think of smart ways around programming? I've been doing this for about a year, and I have improved greatly, but not as much as I should like.

I write all my Ruby programs in SciTe: it's the output that's going to Notepad. I chose Notepad as it is something almost every computer has. But I'll look into Wordpad. Thanks!

···

From: Chad Perrin <perrin@apotheon.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: File Question
Date: Sat, 3 Mar 2007 18:30:50 +0900

On Sat, Mar 03, 2007 at 06:04:00PM +0900, Yannick Grams wrote:
> Chad Perrin wrote:
> > That's a limitation of Notepad.
>
> Do you have any suggestions?

In my personal experience, SciTE is excellent. Even WordPad doesn't
have that limitation (though last I checked it still has a memory leak
that was present in the Write program from which it evolved, back in the
Windows 3.x days -- not my favorite tool for reading text files).

Others may offer other suggestions.

I haven't checked in depth on what exactly limits Notepad -- whether
it suffers hardcoded limitations or the limits have something to do with
system RAM. I just know that significant filesizes and line lengths
tend to cause it to "puke", as 'twere, when other text editing
applications don't have that problem.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Brian K. Reid: "In computer science, we stand on each other's feet."

_________________________________________________________________
Advertisement: Meet Sexy Singles Today @ Lavalife - Click here http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Flavalife9.ninemsn.com.au%2Fclickthru%2Fclickthru.act%3Fid%3Dninemsn%26context%3Dan99%26locale%3Den_AU%26a%3D23769&_t=754951090&_r=endtext_lavalife_dec_meet&_m=EXT

Well, I'm not interested in programming seriously. I just do it as a hobby. But thanks again. Also, one quick (hopefully last) quick question:

I attempted the first part of the code that you gave me, the CHARACTERS array.

CHARBLOCK = {
  ?a = [
    "...@@@....",
    ".@@@@@@@..",
    "@@@...@@@.",
    "@@@@@@@@@.",
    "@@@...@@@.",
    "@@@...@@@."
  ],
  ?b = [
    "...@@@@@@.",
    ".@@@@@@@@.",
    "@@@...@@@.",
    "@@@...@@..",
    "@@@@@@@...",
    "@@@@@@@@..",
    "@@@...@@@.",
    "@@@@@@@@..",
    "@@@@@@...."
  ],
  ?c = [
    "...@@@@@@.",
    ".@@@@@@@@.",
    "@@@...@@@.",
    "@@@.......",
    "@@@...@@@.",
    "@@@@@@@@..",
    "@@@@@@...."
  ],
                #and so on and so forth
}

However, I'm getting errors such as:
rb:2 odd number list for Hash ?a = [
rb:2 syntax error, unexpected "=", expecting "')"
rb:20 syntax error, unexpected ",", expecting $end

I'm not sure what I've done wrong here. If you could help me out one last time, then I'd be very grateful. Thanks in advance.

···

From: Robert Klemme <shortcutter@googlemail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Smart Way Thinking (was: Re: File Question)
Date: Sat, 3 Mar 2007 20:10:05 +0900

On 03.03.2007 11:41, Yannick Grams wrote:

Thankyou very much for the helpful hints. A quick question: how long does it take until one is able to think of smart ways around programming? I've been doing this for about a year, and I have improved greatly, but not as much as I should like.

Um, that's a much more difficult question than the other one. :slight_smile: The general answer to this is probably (as often) "it depends". For me it certainly took a degree in computer science plus several years software development and probably also several programming languages. Others might get the hang of it faster or slower - that probably depends on yourself and your occupation (doing software development full time certainly helps) and what other factors you can think of.

Studying CR certainly helps as this will provide you with some basic concepts and basic understanding (for example, estimating algorithmic complexity) but that brings you only half there. I do not know a proper replacement for experience, as you can learn all the principles in theory but you will be able to apply them properly only with experience. Also, there is a ton of other issues in practice that the university did not teach us at the time I was attending.

The single most important concept in software engineering is IMHO abstraction. By abstraction I mean the way how you distribute functionality across a system (this holds true for large as well small pieces of software). Create artifacts (classes, methods, functions) that do one thing properly. The hard bit is often to determine what this "one thing" is. :slight_smile:

Having said that, you should probably be a bit more patient. :slight_smile:

Kind regards

robert

_________________________________________________________________
Advertisement: It's simple! Sell your car for just $20 at carsales.com.au http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fsecure-au.imrworldwide.com%2Fcgi-bin%2Fa%2Fci_450304%2Fet_2%2Fcg_801577%2Fpi_1005244%2Fai_838588&_t=757768878&_r=endtext_simple&_m=EXT

I write all my Ruby programs in SciTe: it's the output that's going to
Notepad.

I thought the output goes into a textfile. Do you actually call notepad from
inside your script?

I chose Notepad as it is something almost every computer has.

Every computer running Windows anyway.

···

Am Samstag, 3. März 2007 10:32:35 schrieb Yannick Grams:

--
NP: Kataklysm - The Resurrected
Ist so, weil ist so
Bleibt so, weil war so

My fault, I should have tested the code. You need "=>" instead of "=":

CHARBLOCK = {
     ?a => [
         "...@@@....",
         ".@@@@@@@..",
         "@@@...@@@.",
         "@@@@@@@@@.",
         "@@@...@@@.",
         "@@@...@@@."
     ],
     ?b => [
...

  robert

···

On 03.03.2007 12:18, Yannick Grams wrote:

Well, I'm not interested in programming seriously. I just do it as a hobby. But thanks again. Also, one quick (hopefully last) quick question:

I attempted the first part of the code that you gave me, the CHARACTERS array.

Thankyou very much for all your help! You've saved me a lot of copying and pasting. Have a great day!

···

From: Robert Klemme <shortcutter@googlemail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: Smart Way Thinking (was: Re: File Question)
Date: Sat, 3 Mar 2007 20:55:06 +0900

On 03.03.2007 12:18, Yannick Grams wrote:

Well, I'm not interested in programming seriously. I just do it as a hobby. But thanks again. Also, one quick (hopefully last) quick question:

I attempted the first part of the code that you gave me, the CHARACTERS array.

My fault, I should have tested the code. You need "=>" instead of "=":

CHARBLOCK = {
    ?a => [
        "...@@@....",
        ".@@@@@@@..",
        "@@@...@@@.",
        "@@@@@@@@@.",
        "@@@...@@@.",
        "@@@...@@@."
    ],
    ?b => [
...

robert

_________________________________________________________________
Advertisement: Fresh jobs daily. Stop waiting for the newspaper. Search Now! www.seek.com.au http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn.seek.com.au&_t=757263760&_r=Hotmail_EndText_Dec06&_m=EXT