Reputation: 1412
I want to do something like:
for (x in {a,b}) {
...
}
Is there a way to do this in awk?
Upvotes: 0
Views: 164
Reputation: 203254
Two options:
1)
for (x in a)
u[x]
for (x in b)
u[x]
for (x in u)
print "Union Index:",x
2)
for (x in a)
print "Union Index:",x
for (x in b)
if (!(x in a))
print "Union Index:",x
and if you want something you can use succinctly in a for loop:
$ cat tst.awk
function indices(a,b,u, x,c)
{
for (x in a) {
u[++c] = x
}
for (x in b) {
if (!(x in a)) {
u[++c] = x
}
}
return c
}
BEGIN {
a[3]="foo"
a[9]=3
b[5]=7
b[15]=45
for (i=1; i<=indices(a,b,u); i++) {
print u[i]
}
}
$ awk -f tst.awk
9
3
5
15
Upvotes: 3