Ed Morton wrote:
> On 2/8/2008 11:11 AM, Janis Papanagnou wrote:
[snip]
>>
>>You can shorten that a bit without sacrificing the action block by
>>
>> awk '{ sub(/./,NR) } 1' file
>>
>>Personally I consider the concatenation of sub() and 1
>>
>> awk 'sub(/./,NR) 1' file
>>
>>as a hack; it's an unnecessary level of obfuscation[*].
>
>
> I'm all about conciseness. Within that, normally, I'd trade clarity over
brevity
> if there is a trade-off as in this case BUT in this particular case, I
really
> like favoring brevity since it's a very small script so there isn't a
lot of
> clutter with other details and to understand what awk is doing in that
script,
> you have to understand how awk works in terms of condition-action
segments,
> string concatenation, default actions, and return codes so to understand
what
> the above is doing requires you to know a lot that you SHOULD know
anyway before
> doing any awk programming.
>
> So, I like the above because if you understand the awk paradigm already,
the
> script is perfectly clear and if you don't then the small amount of
effort
> required to learn how that small script works takes you a long way to
that
> understanding.
Oh, I don't think that learning a couple characters of "awk paradigm"
would be a problem, and especially not for experienced awk programmers.
The expression is concise enough to be memorized as a "programming
pattern"; and, yes, even for newbies.
You know that I have no problem understanding all those expressions.
My critiques is the unnecessary complexity of unnecessarily involved
concepts in that expression; mainly the implicit conversion. Maybe I
shouldn't have put the point in my footnote, so I place it here...
>> [*] Mixing integral expressions and "invisible" operators, having
>> implicit type conversions, and just for a boolean condition result.
It's actually the implicit complexity of
int(concat(string(sub(...)), string(1)))
that annoys me. (Others might not mind about such internals, though.)
>>A bit more
>>verbose but IMO conceptually clearer (no implicit casts) might be
>>
>> awk 'sub(/./,NR) || 1' file
Which is just
or(sub(...), 1)
all done within the integer domain.
> That seems confusing to me as I can't see any benefit to it over any
other
> approach so I think people would waste their time trying to figure out
what the
> benefit is.
Consider it as a programming pattern, as you do with the textually
more concise concatenation pattern.
> If you're going to do that, IMHO you'd be better off with:
>
> awk '{sub(/./,NR);print}' file
> or
> awk '{sub(/./,NR)}1' file
>
> as they're both clearer and about the same length.
Yes, I agree.
Janis
> Regards,
>
> Ed
[snip]


|