0

Is this a good way to allow an optional user argument ngt to an awk function ?

function nbstring(str, ngt) {
  ## Test whether string STR is non-blank.
  ## Return true when STR is non-blank.
  if (length(gensub(/[[:blank:]]+/, "", "g", str)) > 0) {
   if ( ngt == "dump" ) { print "return 1 for " str }
   return 1
  } else {
   if ( ngt == "dump" ) { print "return 0 for " str }
   return 0
  }
 }

I have also used the condition

if (str && length(gensub(/[[:blank:]]+/, "", "g", str)) > 0) {

and I get the same result.

Thusly, I do not think that doing str && is needed, right?

7
  • 3
    Try that last piece of code with str being a non-empty non-blank string such as 0.
    – Kusalananda
    Commented Feb 28, 2023 at 19:52
  • When I pass nv = 0, I get return 0. But then nv is not a string. Your test is too cryptic, and do not understand its intention.
    – Vera
    Commented Feb 28, 2023 at 20:42
  • I still think that I should not use the additional condition str &&.
    – Vera
    Commented Feb 28, 2023 at 20:52
  • What was your intent in adding str &&?
    – Ed Morton
    Commented Feb 28, 2023 at 21:20
  • 1
    My comment at the top was just intended to show that if you use a variable's value as a boolean test, the value 0 is false, which means that its not an adequate test for "string is not empty".
    – Kusalananda
    Commented Mar 1, 2023 at 3:57

2 Answers 2

3

Are you just trying to test if a string contains any non-blanks? That'd be this in any POSIX awk (no need for the non-POSIX gensub(), nor any other *sub() function, nor a call to length()):

function nbstring(str, ngt,    rslt) {
  ## Test whether string STR is non-blank.
  ## Return true when STR is non-blank.
  rslt = ( str ~ /[^[:blank:]]/ ) 
  if ( ngt == "dump" ) {
    printf "return %d for %s\n", rslt, str
  }
  return rslt
}
7
  • Or to count the non-blanks, n = gsub (/[^[:blank:]]/, "&", str); Commented Mar 1, 2023 at 9:53
  • No, want to test if it is only blanks with no useful information.
    – Vera
    Commented Mar 1, 2023 at 20:15
  • Are you commenting on my answer or responding to @Paul_Pedant's comment?
    – Ed Morton
    Commented Mar 1, 2023 at 21:43
  • My bad. @EdMorton 's answer stops parsing at the first non-blank, and returns a true (1) value for it. My comment counts all non-blanks (so parses the whole string), and returns that count (e.g.4) which is also truthy. So equivalent test, but slower and more obscure. Commented Mar 2, 2023 at 0:18
  • @EdMorton How can I change str ~ /[^[:blank:]]/ to test if str is all blanks ?
    – Vera
    Commented Mar 4, 2023 at 9:33
0

If you're looking for idiomatic awk code I could offer this, but it's not really much different to your own suggestion,

function nbstring(str, ngt,  x) {
    x = (length(gensub(/[[:blank:]]+/, "", "g", str)) > 0);
    if (ngt == "dump") { printf "return %d for < %s >\n", x, str };
    return x;
}

You can call nbstring(str, ngt) or nbstring(str), or indeed nbstring(), as there is no counting of arguments to a function.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .