home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bcars267!bcarh803!christo
- From: christo@bcarh803.BNR.CA (Mark Christopher)
- Subject: Re: AWK and arrays
- Message-ID: <1992Sep4.184556.6202@bnr.ca>
- Sender: news@bnr.ca (usenet)
- Nntp-Posting-Host: bcarh803
- Reply-To: christo@bnr.ca
- Organization: Bell-Northern Research Ltd.
- References: <1992Sep4.030733.27724@newshost.anu.edu.au>
- Date: Fri, 4 Sep 1992 18:45:56 GMT
- Lines: 46
-
- In article <1992Sep4.030733.27724@newshost.anu.edu.au>,
- aps583@huxley.anu.edu.au (Anthony P Scott) writes:
- |>Suppose I want to read a bunch of letters one to a line
- |>and output the position in the alphabet that they occupy.
- |>
- |>ie
- .. replace 'a' with '1', 'b' with '2', 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
-
- You need to say: alph["a"] = 1; alph["b"] = 2; alph["c"] = 3; etc...
-
- |>and then read the lines of text as another array called charact[NR]
-
- Why do you need to save them in an array? Couldn't you just do:
-
- { print alph[$1] }
-
- |>and the use the charact array to subscript the alph array ie
- ...
- the code didn't work because you didn't initialize your array "alph"
- correctly.
-
- What would be wrong with:
- {
- print index("abcdefghijklmnopqrstuvwxyz",substr($1,1,1))
- }
-
- This would print 0 if the first letter of the first arg was not a
- lowercase letter.
-
-
-
- |>I hope this is not a silly question.
-
- No such thing as a silly question. Learn how to find the answers
- yourself. Read the man page, read the book, see the movie. You
- will find the answers faster.
-
- --
- Mark Christopher Bell-Northern Research christo@bnr.ca
-