Re: How to rearrange text fields in sed?



2006-01-17, 14:29(-06), Ed Morton:
[...]
>>>>>>>awk 'ORS=(NR%4?" ":"\n")'
[...]
> I disagree. It's more like if in C I had written something concise like:
>
> done = (argc > 3 ? 1 : 0);
>
> instead of:
>
> if (argc > 3)
> {
> done = 1;
> }
> else
> {
> done = 0;
> }
>
> Yes, to anyone who doesn't know C the first form is slightly less
> readable BUT it's perfectly readable to anyone who does know the
> language and is a perfectly reasonable way to write the code.
[...]

It's not. In the C code above, you're using the ternary operator
for what it is meant to be used for which is perfectly fine even
if less readable than the longer form. You'll see that many
company have "general coding guidelines" that prohibit the use
of that ternary operator, though.

In

awk 'ORS=(NR%4?" ":"\n")'

Though it's a nice "trick", it is not good coding practice,
because:

- it puts the action in the condition part (the original awk
wouldn't even have allowed you to do that).
- it relies on a not straightforward side effect: the fact that
both " " and "\n" resolve to true. Actually, some of your
past posts that had that same kind of construct suffered from
bugs where the expression didn't resolve to true in corner
cases.
- the action is not clearly visible. It's the implicit {print},
but at first sight it looks like it's an assignment.
- it's misleading. As in my C code above, using "=" in a
condition may mislead one that could take it for a "=="
- it doesn't tell in "awk code" what you had in mind, or at
least what the algorithm is. The algorithm is: print the
current record, if the line number is a multiple of 4 append a
newline, append " " otherwise. Instead, your code translates
into "print the current record (implied with the current ORS
appended) only if the assignment to the ORS special variable
of a string that is "\n" if NR is a multiple of 4 or " "
otherwise resolves to true".
- it's unnecessarily compact.

I wouldn't have complained on:

awk '
{
ORS = (NR % 4 == 0 ? "\n" : " ")
print
}'

or if you hadn't made a point on its /maintainability/.

--
Stéphane
.



Relevant Pages

  • Re: Generic Functions
    ... Having defined x as default real, any assignment to x MUST resolve to type ... either by explicit or implicit type casting. ... style choice, but it is a style choice - not a dictate of the language. ...
    (comp.lang.fortran)
  • Re: If I want to put an atom at the end of a list, which is better ?
    ... reorganize your algorithm so that the list traversals inherent in append ... the list that you want to append ... The end of a (proper) list is always NIL. ... possible to have a list ending in an atom, but then it is not a proper ...
    (comp.lang.lisp)
  • Re: List Performance
    ... optimization for dealing with long list, to replace an algorithm with a ... lot of append by something like this: ... mark = object ... has been a standard idiom since before objectexisted;-) ...
    (comp.lang.python)
  • Re: Opening WebPage in Maintenance View
    ... I had exact IP address assignment. ... I can open default.aspx of the subsite of that subsite ... >> Mike Walsh, Helsinki, Finland ... >>> This resolve my problem. ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Complexity of a specific kind of instances of a NP-complete problem
    ... > |If we don't expect to resolve all the instances of TSP ... > There's a kind of universal searching algorithm known as ... > in Otime, then Levin search does too, but with the constant ... > fraction of its time simulating this other algorithm. ...
    (comp.theory)

Loading