bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Suggestion to simplify one of the examples


From: Wolfgang Laun
Subject: Re: [bug-gawk] Suggestion to simplify one of the examples
Date: Sun, 16 Jun 2019 20:58:30 +0200

Neither version is a good example for illustrating the getline function
because getline isn't necessary to solve the task and both contain too much
"noise".

Although not very useful (is the present example "useful"?), this is short
and illustrates all the original example does.
# swap successive lines
{
   line2 = $0
   if (getline <= 0) {
       print("unexpected EOF or error:", ERRNO) > "/dev/stderr"
       exit
   }
   print $0
   print line2
}

For removing /*...*/ with at most one /* per line, a more awk-ish program
(without getline) could be written. This is left as an exercise etc.

-W

On Sun, 16 Jun 2019 at 17:22, M <address@hidden> wrote:

> When rereading the last version of the manual, I noticed vague operations
> with a variable "j" in the example "Remove text between /* and */,
> inclusive" (4.10.1 Using getline with No Arguments).
> If we admit that there are some drawbacks (e.g. the program doesn't work
> if one comment ends and another begins on the same line, it produces
> "substr" boundary warnings, and so on), why don't we write in this way:
>
> # Remove text between /* and */, inclusive
> {
>    if ((i = index($0, "/*")) != 0) {
>       out = substr($0, 1, i - 1)  # leading part of the string
>       rest = substr($0, i + 2)    # ... */ ...
>       while ((j = index(rest, "*/")) == 0) {  # is */ in trailing part?
>          # get more text
>          if (getline <= 0) {
>             print("unexpected EOF or error:", ERRNO) > "/dev/stderr"
>             exit
>          }
>          # build up the line using string concatenation
>          rest = rest $0
>       }
>       rest = substr(rest, j + 2)  # remove comment
>       # build up the output line using string concatenation
>       $0 = out rest
>    }
>    print $0
> }
>
> ##############################
>
> ?
>
> Indeed, the goal is to show the usage of "getline", not to demonstrate
> e.g. control statements or "break" statement.
>
> _______________
>
> Yours respectfully,
> Mark Krauze
>
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]