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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bcars267!bcarh803!christo
  3. From: christo@bcarh803.BNR.CA (Mark Christopher)
  4. Subject: Re: AWK and arrays
  5. Message-ID: <1992Sep4.184556.6202@bnr.ca>
  6. Sender: news@bnr.ca (usenet)
  7. Nntp-Posting-Host: bcarh803
  8. Reply-To: christo@bnr.ca
  9. Organization: Bell-Northern Research Ltd.
  10. References:  <1992Sep4.030733.27724@newshost.anu.edu.au>
  11. Date: Fri, 4 Sep 1992 18:45:56 GMT
  12. Lines: 46
  13.  
  14. In article <1992Sep4.030733.27724@newshost.anu.edu.au>,
  15. aps583@huxley.anu.edu.au (Anthony P Scott) writes:
  16. |>Suppose I want to read a bunch of letters one to a line
  17. |>and output the position in the alphabet that they occupy.
  18. |>
  19. |>ie
  20. .. replace 'a' with '1', 'b' with '2', etc...
  21.  
  22. |>One way I visualized doing this is to write an array called alph[letter]
  23. |>such as:
  24. |>
  25. |>alph[a] =1
  26. |>alph[b] = 2
  27. |>alph[c] = 3
  28.  
  29. You need to say:  alph["a"] = 1; alph["b"] = 2; alph["c"] = 3; etc...
  30.  
  31. |>and then read the lines of text as another array called charact[NR]
  32.  
  33. Why do you need to save them in an array?  Couldn't you just do:
  34.  
  35.     { print alph[$1] }
  36.  
  37. |>and the use the charact array to subscript the alph array ie
  38. ...
  39. the code didn't work because you didn't initialize your array "alph"
  40. correctly.
  41.  
  42. What would be wrong with:
  43.     {
  44.         print index("abcdefghijklmnopqrstuvwxyz",substr($1,1,1))
  45.     }
  46.  
  47. This would print 0 if the first letter of the first arg was not a
  48. lowercase letter.
  49.  
  50.  
  51.  
  52. |>I hope this is not a silly question.
  53.  
  54. No such thing as a silly question.  Learn how to find the answers
  55. yourself.  Read the man page, read the book, see the movie.  You
  56. will find the answers faster.
  57.  
  58. -- 
  59. Mark Christopher        Bell-Northern Research          christo@bnr.ca
  60.