The [tt]substr[/tt] method is the only one that does what the original poster asked for (making some assumptions about the English).
That doesn't mean that's the best way to get the job done, or even that it works in all required cases. The input data format isn't described well enough to know the "best" way of processing it.
If the original request is accurate, then the [tt]substr[/tt] method is a simple, elegant way of solving the problem.
In other words, if the English description of the problem is "Replace characters 34-36 with characters 7-9 in records where the first field starts with 'A'," then using [tt]substr[/tt] is certainly not a kludge. The problem calls for something that works with substrings at given character positions; that's what [tt]substr[/tt] does.
Now, it's possible that the original poster oversimplified the problem, and a proper description of the problem would be more like "When the first character of the first field is 'A', append characters 7-9 of the first field to the second field. In that case, a [tt]substr[/tt]-based solution probably wouldn't cut it, and a field-based on would be better.
futurelet said:
To make code more pleasing (and more robust), one avoids or at least hides gory details and operates at a higher level of abstraction.
Unless the problem can only be stated in terms of gory details, in which case dealing with gory details is necessary.
futurelet said:
It is preferable to say "take field 4" rather than to say "start at the 39th character and take the next 7 characters".
Not if we trust the original problem description. Again, if it is accurate, then the field-based solution may not be preferable, and may even be incorrect. (This would leave the input format open to criticism, but that's beside the point).
futurelet said:
Saying "overlay xyz at column 35" is more specific and less abstract than saying "the fields are separated by whitespace; append xyz to the 2nd field". Therefore, I prefer the latter. And it relieves one from having to figure out the correct column number!
That's a a good way of thinking, and if it can be applied to this problem, then it probably should be.
Suggesting the field-based method was a great idea as it gives the original poster a chance to consider whether this may in fact be a better way to solve the problem.
However, arguing about which solution is "preferable" is futile when the input format, and therefore the problem itself, hasn't been stated clearly.
Effectively, you're arguing that the solution to one problem is better than another solution to a different problem, and you have no evidence that the two problems are the same problem in the first place.
What I believe you want to be saying is that the
input format should have been designed so that a field-based method would be sufficient for processing it, and that if it has indeed beeen designed so that the column number matters, then it has been poorly designed.
Most people would agree with you there. However, we unfortunately have to solve the problems that happen, not the ones that should have happened.
Or perhaps you mean to suggest that the original poster should consider restating the problem so that it involves fields instead of character positions.
That's probably a good idea, too.
Plus, if you got confirmation that a field-based method would solve the problem as effectively as a[tt]substr[/tt]-based one, you'd be in position to have an argument about which solution is better.
Arguments are fine, but they never go anywhere unless you actually argue about the same thing.