My noob "numbering lines" programe. How to improve?

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

heres my version

#program to insert numbers infront of lines.
def numbertext(infilename, outfilename)
#for neat formating work out how many lines there are first so we know
how #many digits to set ljust to
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

linenumber = 0
File.open(outfilename, “w”) do |output|
File.foreach(infilename) do |input|
output.write linenumber.to_s.rjust(numdigits) + " " + input
linenumber = linenumber + 1
end
end
end

tsk tsk! where are my manners - it should read PLEASE give us your
hints.

On Feb 13, 3:50 pm, Adam A. [email protected] wrote:

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

Here are some suggestions:

  • Use the += and *= compound assignment operators
  • Use << instead of .write
  • “%10s” % str is like rjust, so
  • “%#{numdigits}s %s” % [ linenumber, input ]
  • Look at each_with_index
  • Think about a better way to calculate numdigits; e.g.:
    • Math.log10( max ).ceil
    • total_lines.to_s.size

Hi –

On 13/02/2008, Adam A. [email protected] wrote:

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

If all you’re doing is inserting line numbers in front of the lines in
that file then:

f=File.open(“./file”).readlines
lines=0
f.each {|content| puts “#{lines += 10}: #{content}” }’

In its crudest form will do that, but note that it doesn’t modify
anything, it just dumps the values out. One other approach is to
modify an array of those values “in-place” instead, as in:

f=File.open(“./.xsession”).readlines
f.each {|e| e.sub!(/^/, "#{f.index(e) + 10}: ")}
puts f

This “cheats” and uses the index of the element to form the line
number - adding it to the start of the string element.

Saving the resultant array “f” to a file, per the second example is
left an exercise to the reader, as is any further cosmetic formatting
to the output.

– Thomas A.

On Feb 13, 2008, at 5:50 PM, Adam A. wrote:

#for neat formating work out how many lines there are first so we
know
how #many digits to set ljust to
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

Since the representation of numbers doesn’t shrink and you already
have totalnumlines (which would typically be total_num_lines), perhaps
you could think of a direct way of finding the number of character
positions that you’d need to reserve for this largest number.

linenumber = 0
File.open(outfilename, “w”) do |output|
File.foreach(infilename) do |input|
Too bad you didn’t save the lines when you read the file the first
time… then you could have used .each_with_index. You could even use
$. to get the current line number from the file rather than keep your
own explicit counter in linenumber. (Which would be idiomatically
line_number += 1)

 output.write linenumber.to_s.rjust(numdigits) + " " + input
 linenumber = linenumber + 1

end
end
end

All hints, no code. :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On 13/02/2008, Adam A. [email protected] wrote:

numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

you don’t need such a loop. this can be replaced with

numdigits = Math.log10(totalnumlines).to_i+1

-Thomas


Thomas P.
[email protected]
[email protected]
Büro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06

http://www.thopre.com/

Adam A. wrote:

totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1

  1. Suppose totalnumlines = 3. The last time through your while loop, x
    will equal 2–if x is equal to 3, the loop will terminate and not
    execute again. So the assignment to x inside the loop will assign 2*10=
    20 to x. So why not just do this:

x = (totalnumlines - 1) * 10

There’s no need for any loop to calculate x.

  1. Once again suppose totalnumlines = 3. That means the loop will
    execute twice: the first time when x =1 and the second time when x=2.
    When x =3, the loop will terminate before it executes again. Therefore,
    numdigits will equal 3, which is the same value as totalnumlines. So
    you can just write:

numdigits = totalnumlines

2008/2/14, Phrogz [email protected]:

  • Use << instead of .write
  • “%10s” % str is like rjust, so
  • “%#{numdigits}s %s” % [ linenumber, input ]

Or rather

“%*d %s” % [numdigits, linenumber, input ]
printf “%*d %s”, numdigits, linenumber, input

  • Look at each_with_index
  • Think about a better way to calculate numdigits; e.g.:
    • Math.log10( max ).ceil
    • total_lines.to_s.size
  • Look at ARGF and ARFG.lineno

Kind regards

robert

On Feb 13, 4:50 pm, Adam A. [email protected] wrote:

#for neat formating work out how many lines there are first so we know
File.open(outfilename, “w”) do |output|
File.foreach(infilename) do |input|
output.write linenumber.to_s.rjust(numdigits) + " " + input
linenumber = linenumber + 1
end
end
end

Posted viahttp://www.ruby-forum.com/.

a = IO.readlines(‘data’)
puts (1…a.size).map{|x| x.to_s.rjust(a.size.to_s.size)}.zip(a).
map{|x| x.join ’ '}

On 2/14/08, Thomas P. [email protected] wrote:

you don’t need such a loop. this can be replaced with

numdigits = Math.log10(totalnumlines).to_i+1

Hint:
why do math to calculate how many digits it takes to represent a
number as a string, when you can just let the String class tell you?

On 14/02/2008, Adam S. [email protected] wrote:

why do math to calculate how many digits it takes to represent a
number as a string, when you can just let the String class tell you?

every child can count the digits in a string, but to prove that you have
understood logarithms you need another solution … :wink:

-Thomas


Thomas P.
[email protected]
[email protected]
Büro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06

http://www.thopre.com/