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
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.:
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:
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.
#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)
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 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.
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:
#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 ’ '}