home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!munnari.oz.au!manuel!huxley!aps583
- From: aps583@huxley.anu.edu.au (Anthony P Scott)
- Subject: AWK and arrays
- Message-ID: <1992Sep4.030733.27724@newshost.anu.edu.au>
- Originator: aps583@huxley
- Sender: news@newshost.anu.edu.au
- Organization: Australian National University
- Date: Fri, 4 Sep 92 03:07:33 GMT
- Lines: 63
-
- Suppose I want to read a bunch of letters one to a line
- and output the position in the alphabet that they occupy.
-
- ie
-
- a
- b
- c
- d
- e
-
- would be output as
-
- 1
- 2
- 3
- 4
- 5
-
- etc.
-
- One way I visualized doing this is to write an array called alph[letter]
- such as:
-
- alph[a] =1
- alph[b] = 2
- alph[c] = 3
- ...
-
- ...
- alph[z] = 26
-
- and then read the lines of text as another array called charact[NR]
-
- ie
-
- {
- charact[NR] = $1
- }
-
- and the use the charact array to subscript the alph array ie
-
- { for (i=1; i<=NR; i++) {
- char_num[i] = alph[charact[i]] }
- }
-
- This doesn't work. Can anyone point me in the right direction
-
- Another way I thought of doing the job was to get the subscript number of
- an array item.
- Something like:
-
- for (charact[i] in alph) {
- print alph[i]
- }
-
- This doesn't work either.
-
- I hope this is not a silly question.
-
- Regards
-
- Tony Scott
-