bug-gawk
[Top][All Lists]
Advanced

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

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


From: M
Subject: [bug-gawk] Suggestion to simplify one of the examples
Date: Sun, 16 Jun 2019 18:22:15 +0300

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]