home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / misc / 2930 < prev    next >
Encoding:
Text File  |  1992-09-03  |  1.9 KB  |  85 lines

  1. Newsgroups: comp.lang.misc
  2. Path: sparky!uunet!wupost!darwin.sura.net!jvnc.net!princeton!att!cbnewsm!sani
  3. From: sani@att.com (Sani Nassif [227])
  4. Subject: Re: AWK using an array to subscript an array??
  5. Organization: AT&T
  6. Date: Fri, 4 Sep 1992 11:51:39 GMT
  7. Message-ID: <1992Sep4.115139.19679@cbnewsm.cb.att.com>
  8. X-Newsreader: Tin 1.1 PL5
  9. References: <1992Sep4.004020.26667@newshost.anu.edu.au>
  10. Sender: news@cbnewsm.cb.att.com (NetNews Administrator)
  11. Nntp-Posting-Host: merlot.cnet.att.com
  12. Lines: 71
  13.  
  14. Anthony P Scott (aps583@huxley.anu.edu.au) wrote (with some edits):
  15. : Say for instance that I wanted to read a line of text containing
  16. : one character and output that line in terms of the postion of the
  17. : letters in the alphabet.
  18. : ie  like:
  19. :  a
  20. :  b
  21. :  c
  22. :  d
  23. : would be printed as:
  24. :  1
  25. :  2
  26. :  3
  27. :  4
  28. : One way I visualized doing this is to read the alphabet into an array
  29. : such as:
  30. : alph[a] = 1
  31. : alph[b] = 2
  32. : alph[c] = 3
  33. : ...
  34. : and then read the line of text into another array by
  35. : {
  36. :     line_arr[NR] = $1
  37. : }
  38. :  and then using something like
  39. : for (i=1; i<=NR; i++) {
  40. :     alph_pos[i] = alph[line_arr[i]]
  41. :     print alph_pos[i]
  42. : }
  43. : However this doesn't work. Does any body have any suggestions?
  44. : Thanks in anticipation.
  45. : Tony Scott
  46.  
  47. For one thing, you might want to try to have the array contain
  48. the "string" alphabets:
  49.  
  50.  alph["a"] = 1
  51.  alph["b"] = 2
  52.  alph["c"] = 3
  53.  
  54. Otherwist, AWK will just put zeros for a, b, c assuming they are
  55. variables. In general, a program to do this might look like:
  56.  
  57. BEGIN    {
  58.     while (getline < INDEXFILE > 0) {
  59.     index[$1] = ++N
  60.     }
  61. }
  62.  
  63. {
  64.     print index[$1]
  65. }
  66.  
  67. where INDEXFILE is passed in pointing to some file that has
  68. your "alphabet".
  69.  
  70. Sani.
  71. ----------------------------------------------------------------------
  72. Sani R. Nassif
  73. (sani.nassif@att.com)
  74. ----------------------------------------------------------------------
  75.