🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #1120

Re: AWK using an array to subscript an array??

#1120
From: Sani Nassif [227]
Date: Fri, 04 Sep 1992 11:51
71 lines
1985 bytes
Anthony P Scott (aps583@huxley.anu.edu.au) wrote (with some edits):
: Say for instance that I wanted to read a line of text containing
: one character and output that line in terms of the postion of the
: letters in the alphabet.
: 
: ie  like:
:  a
:  b
:  c
:  d
: 
: would be printed as:
:  1
:  2
:  3
:  4
: 
: One way I visualized doing this is to read the alphabet into an array
: such as:
: 
: alph[a] = 1
: alph[b] = 2
: alph[c] = 3
: ...
: 
: and then read the line of text into another array by
: 
: {
:     line_arr[NR] = $1
: }
: 
:  and then using something like
: 
: for (i=1; i<=NR; i++) {
:     alph_pos[i] = alph[line_arr[i]]
:     print alph_pos[i]
: }
: 
: However this doesn't work. Does any body have any suggestions?
: Thanks in anticipation.
: 
: Tony Scott

For one thing, you might want to try to have the array contain
the "string" alphabets:

 alph["a"] = 1
 alph["b"] = 2
 alph["c"] = 3

Otherwist, AWK will just put zeros for a, b, c assuming they are
variables. In general, a program to do this might look like:

BEGIN	{
    while (getline < INDEXFILE > 0) {
	index[$1] = ++N
    }
}

{
    print index[$1]
}

where INDEXFILE is passed in pointing to some file that has
your "alphabet".

Sani.
----------------------------------------------------------------------
Sani R. Nassif
(sani.nassif@att.com)
----------------------------------------------------------------------
Technical Details
Message-ID: <1992Sep4.115139.19679@cbnewsm.cb.att.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!darwin.sura.net!jvnc.net!princeton!att!cbnewsm!sani
References: <1992Sep4.004020.26667@newshost.anu.edu.au>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.misc
Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!darwin.sura.net!jvnc.net!princeton!att!cbnewsm!sani
From: sani@att.com (Sani Nassif [227])
Subject: Re: AWK using an array to subscript an array??
Organization: AT&T
Date: Fri, 4 Sep 1992 11:51:39 GMT
Message-ID: <1992Sep4.115139.19679@cbnewsm.cb.att.com>
X-Newsreader: Tin 1.1 PL5
References: <1992Sep4.004020.26667@newshost.anu.edu.au>
Sender: news@cbnewsm.cb.att.com (NetNews Administrator)
Nntp-Posting-Host: merlot.cnet.att.com
Lines: 71