A
Adam Akhtar
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
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