home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / awklib / eg / lib / ord.awk < prev    next >
Text File  |  1997-03-15  |  1KB  |  55 lines

  1. # ord.awk --- do ord and chr
  2. #
  3. # Global identifiers:
  4. #    _ord_:        numerical values indexed by characters
  5. #    _ord_init:    function to initialize _ord_
  6. #
  7. # Arnold Robbins
  8. # arnold@gnu.ai.mit.edu
  9. # Public Domain
  10. # 16 January, 1992
  11. # 20 July, 1992, revised
  12.  
  13. BEGIN    { _ord_init() }
  14. function _ord_init(    low, high, i, t)
  15. {
  16.     low = sprintf("%c", 7) # BEL is ascii 7
  17.     if (low == "\a") {    # regular ascii
  18.         low = 0
  19.         high = 127
  20.     } else if (sprintf("%c", 128 + 7) == "\a") {
  21.         # ascii, mark parity
  22.         low = 128
  23.         high = 255
  24.     } else {        # ebcdic(!)
  25.         low = 0
  26.         high = 255
  27.     }
  28.  
  29.     for (i = low; i <= high; i++) {
  30.         t = sprintf("%c", i)
  31.         _ord_[t] = i
  32.     }
  33. }
  34. function ord(str,    c)
  35. {
  36.     # only first character is of interest
  37.     c = substr(str, 1, 1)
  38.     return _ord_[c]
  39. }
  40. function chr(c)
  41. {
  42.     # force c to be numeric by adding 0
  43.     return sprintf("%c", c + 0)
  44. }
  45. #### test code ####
  46. # BEGIN    \
  47. # {
  48. #    for (;;) {
  49. #        printf("enter a character: ")
  50. #        if (getline var <= 0)
  51. #            break
  52. #        printf("ord(%s) = %d\n", var, ord(var))
  53. #    }
  54. # }
  55.