Thread View: comp.lang.misc
2 messages
2 total messages
Started by Anthony P Scott
Fri, 04 Sep 1992 00:40
AWK using an array to subscript an array??
Author: Anthony P Scott
Date: Fri, 04 Sep 1992 00:40
Date: Fri, 04 Sep 1992 00:40
75 lines
1200 bytes
1200 bytes
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
e
f
g
h
i
j
k
l
m
n
o
p
q
would be printed as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
...
...
alph[q] = 20
and then read the line of text into another array by
{
line_arr[NR] = $1
}
and then using something like
for (i=i; 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
Re: AWK using an array to subscript an array??
Author: Sani Nassif [227]
Date: Fri, 04 Sep 1992 11:51
Date: Fri, 04 Sep 1992 11:51
71 lines
1985 bytes
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)
----------------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads