APLish AWK functions 
Author Message
 APLish AWK functions

I was asked to post my (gnu) awk functions, so here is the first part.  The
rest aren't fit for public consumption yet...  Eventually I would like to add
replicate (or mesh) as a primitive.


# info:   APLish AWK functions (Part I)
# date:   Last modified September 20, 1994

#
BEGIN { # EXAMPLES
  print repeat(10,"x")
  print repeat(10,"xyz")
  print repeat(10,"abcdefghijklmnopqrstuvwxyz")
  print initial(5)
  split(initial(5),i5)
  print list(i5)
  split(initial(9),i9)
  print list(i9,i5)
  print list(i9,"6 7 8 9")
  print repeat(10,i5)
  delete i5[1]
  print repeat(10,i5)
  print filter("001010001")
  print filter("001010001",i9)
  print list(i9,filter("001010001"))
  print filter("001010001","123456789")
  print head("") head("xxx") head(i9) initial(1,"xxx") initial(1,"") "x"

Quote:
}

function repeat(n,x, i,y,z) # n?x (string or array)  <rho>
{ if (n < 0)
    return ""
  if (emptyarray(x))
  { if (x == "")
      return repeat(n,"          ")
    while (length(z) < n) z = z z x
    return substr(z,1,n)
  }
  else
  { while (n > 0)
      for (i in x)
      { z = z x[i] " "
        if (--n <= 0) break
      }
    return z
  }
Quote:
}

function initial(n,x, i,z) # !n or n^x (string or array)  <iota/take>
{ if (emptyarray(x))
    if (x != "")
      return substr(x,1,n) repeat(n-length(x))
    else
      while (i < n) z = z (++i) " "
  else
    for (i in x)
    { if (n-- <= 0) break
      z = z x[i] " "
    }
  return z
Quote:
}

function list(x,y, i,z) # x,y are string or array or null
{ if (emptyarray(x)) split(x,x)
  if ((y == "") && emptyarray(y))
    for (i in x) z = z x[i] " "
  else
  { if (emptyarray(y)) split(y,y)
    for (i in y) z = z x[y[i]] " "
  }
  return z
Quote:
}

function filter(b,x, i,j,z) # b is string of 01, x is string or array or null
{ if ((x == "") && emptyarray(x))
    while (i = index(b,"1"))
    { z = z i " "
      sub("1","0",b)
    }
  else if (emptyarray(x))
    # this should be added to awk as primitive
    while (i = index(b,"1"))
    { z = z repeat(i-j-1,"__________") substr(x,i,1)
      sub("1","0",b)
      j = i
    }
  else
    while (i = index(b,"1"))
    { z = z x[i] " "
      sub("1","0",b)
    }
  return z
Quote:
}

function head(x, i) # x is string or array or null; head of null is null
{ if (emptyarray(x))
    return substr(x,1,1)
  else
    for (i in x)
      return x[i]
Quote:
}

function emptyarray(x, y)
{ for (y in x)
    return 0
  return 1
Quote:
}



Sun, 09 Mar 1997 06:26:44 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. MMa Boolop Distributivity Problem - Need MMa-like vice APLish Sol'n (6KB)

2. Inverse trigonometric functions in awk

3. return multiple values from an awk function?

4. Format functions in AWK

5. Lookup function in awk - answer

6. Including awk (mawk) functions in a script

7. AWK close function

8. can AWK read function from input file?

9. Unescape function in Awk

10. No ABS function in Awk?

11. Date functions for awk?

12. Function in awk

 

 
Powered by phpBB® Forum Software