I’m stuck on an elegant, i.e., Ruby, way to do the first
two steps. Please point me toward the glittering light.
For 1) Assuming the lines aren’t fixed length: Read in the entire file
into an array (using IO#readlines), in which case you can skip straight
to the line you want, or counting line by line until you find the one
you want.
For 2) Any particular reason why you need to search backwards? My
suggestion would be to look at String#gsub - that will help you handle
both 2) and 3) in one go.
Wait a minute … is the file you’re editing always a Fortran source?
Why not just comment out the offending line rather than removing it?
Otherwise, removing a line changes the position number of every line
following it.
For 1) Assuming the lines aren’t fixed length: Read in the entire file
into an array (using IO#readlines), in which case you can skip
straight
to the line you want, or counting line by line until you find the one
you want.
For 2) Any particular reason why you need to search backwards? My
suggestion would be to look at String#gsub - that will help you handle
both 2) and 3) in one go.
I like this idea, but I would suggest using a hash rather than an
array. Hash the lines keyed to their line number. Then you can search
any which way you like.
I like this idea, but I would suggest using a hash rather than an
array. Hash the lines keyed to their line number. Then you can search
any which way you like.
I don’t see what this would give you. An array of the lines in the
order read would be keyed to the line number. Assuming the Ruby array
implementation is sane using an array should be faster, and also has
the advantage that IO#readlines does all the work of reading the lines
in from file and returning a ready populated array.
I like this idea, but I would suggest using a hash rather than an
array. Hash the lines keyed to their line number. Then you can search
any which way you like.
I don’t see what this would give you. An array of the lines in the
order read would be keyed to the line number. Assuming the Ruby array
implementation is sane using an array should be faster, and also has
the advantage that IO#readlines does all the work of reading the lines
in from file and returning a ready populated array.
It’s a worthless idea. Somedays my brain behaves like a fried egg
even through I don’t do drugs. Today seems to be one of those days
.
I believe that the OP doesn’t want to search line 1707 backwards for
dt, he want’s to search the file backwards starting at line 1707 (or
maybe 1706) for a line containing dt.
Ah! /m
Granted in that case it would be simplest to use #rindex or to reverse
the String altogether.
I believe that the OP doesn’t want to search line 1707 backwards for
dt, he want’s to search the file backwards starting at line 1707 (or
maybe 1706) for a line containing dt.