home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10721 < prev    next >
Encoding:
Text File  |  1992-09-03  |  1.2 KB  |  75 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!munnari.oz.au!manuel!huxley!aps583
  3. From: aps583@huxley.anu.edu.au (Anthony P Scott)
  4. Subject: AWK and arrays
  5. Message-ID: <1992Sep4.030733.27724@newshost.anu.edu.au>
  6. Originator: aps583@huxley
  7. Sender: news@newshost.anu.edu.au
  8. Organization: Australian National University
  9. Date: Fri, 4 Sep 92 03:07:33 GMT
  10. Lines: 63
  11.  
  12. Suppose I want to read a bunch of letters one to a line
  13. and output the position in the alphabet that they occupy.
  14.  
  15. ie
  16.  
  17. a
  18. b
  19. c
  20. d
  21. e
  22.  
  23. would be output as
  24.  
  25. 1
  26. 2
  27. 3
  28. 4
  29. 5
  30.  
  31. etc.
  32.  
  33. One way I visualized doing this is to write an array called alph[letter]
  34. such as:
  35.  
  36. alph[a] =1
  37. alph[b] = 2
  38. alph[c] = 3
  39. ...
  40.  
  41. ...
  42. alph[z] = 26
  43.  
  44. and then read the lines of text as another array called charact[NR]
  45.  
  46. ie
  47.  
  48. {
  49. charact[NR] = $1
  50. }
  51.  
  52. and the use the charact array to subscript the alph array ie
  53.  
  54. { for (i=1; i<=NR; i++) {
  55. char_num[i] = alph[charact[i]] }
  56. }
  57.  
  58. This doesn't work. Can anyone point me in the right direction
  59.  
  60. Another way I thought of doing the job was to get the subscript number of
  61. an array item.
  62.  Something like:
  63.  
  64. for (charact[i] in alph) {
  65.     print alph[i]
  66. }
  67.  
  68. This doesn't work either.
  69.  
  70. I hope this is not a silly question.
  71.  
  72. Regards 
  73.  
  74. Tony Scott
  75.