I have a strange array access problem. The code is
kind of involved, so I appreciate your time in advance.
Here is the operative code..
#deletes array elements -gt num -a -lt max
function kill(arra,num,max) {
if (n > num && n < max) {
n++
delete arra[num]
}
return
}
#returns number of elements in an array
function _elems(arr) {
for (i in arr) {
}
return i
}
#generic swap
function swap(arr,ind,sind) {
arr[ind] = tmp
arr[ind] = arr[sind]
arr[sind] = tmp
return
}
#prints an number indiced array low to high.
function parray(arr,num) {
print "#######################OUT:"
while (x <= num) {
x++
print arr[x]
}
}
#loads a source file
function loader(src, arr,ret,x,i) {
while ((getline < src) > 0) {
arr[i++] = $0
}
close(src)
ret = ""
for (x=0 ; x <= i ; x++) {
ret = length(ret) < 1 ? arr[x] "\n" : ret arr[x] "\n"
}
return ret
}
############main BEGIN() excerpted code############
info = loader(ARGV[1])
params = split(info,array,"\n"
parray(array,params)
if (params) {
for (x = 1 ; x <= params + 1 ; x++) {
if (array[x] ~ /[mM]ode:.*/) {
gsub(/[mM]ode:/,"",array[x])
swap(array,1,x)
}
if (array[x] ~ /[hH]ost:.*/) {
gsub(/[hH]ost:/,"",array[x])
swap(array,2,x)
}
if (array[x] ~ /[pP]ort:.*/) {
gsub(/[pP]ort:/,"",array[x])
swap(array,3,x)
}
}
kill(array,3,(params + 1))
parray(array,3)
print a = _elems(array), "in array."
print "IN#############################IN"
for (all in array) {
print array[all]
}
(The hash marks are part of the sourced file and program output::sorry for the mess.)
Here is the output:
###################OUT:
################
##############
mode:udp
############
host:localhost
###########
port:23-900
###########
::Note->parray() works here.
###################OUT:
::Note->parray() does not work here(?)
3 in array.
::Note->kill() has eliminated the specified elements.
::Should only be three elements now.
IN######################IN
############
###########
###########
udp
localhost
23-900
::note->What(?) That's 10 elements again.
My understanding and practical experience is that the awk
array is not some "protected object" and can be passed
and changed in function calls. What am I missing?
Don't ask what the program does. It is a proof of
concept in gawk 3.1 and is evil.
kind of involved, so I appreciate your time in advance.
Here is the operative code..
#deletes array elements -gt num -a -lt max
function kill(arra,num,max) {
if (n > num && n < max) {
n++
delete arra[num]
}
return
}
#returns number of elements in an array
function _elems(arr) {
for (i in arr) {
}
return i
}
#generic swap
function swap(arr,ind,sind) {
arr[ind] = tmp
arr[ind] = arr[sind]
arr[sind] = tmp
return
}
#prints an number indiced array low to high.
function parray(arr,num) {
print "#######################OUT:"
while (x <= num) {
x++
print arr[x]
}
}
#loads a source file
function loader(src, arr,ret,x,i) {
while ((getline < src) > 0) {
arr[i++] = $0
}
close(src)
ret = ""
for (x=0 ; x <= i ; x++) {
ret = length(ret) < 1 ? arr[x] "\n" : ret arr[x] "\n"
}
return ret
}
############main BEGIN() excerpted code############
info = loader(ARGV[1])
params = split(info,array,"\n"
parray(array,params)
if (params) {
for (x = 1 ; x <= params + 1 ; x++) {
if (array[x] ~ /[mM]ode:.*/) {
gsub(/[mM]ode:/,"",array[x])
swap(array,1,x)
}
if (array[x] ~ /[hH]ost:.*/) {
gsub(/[hH]ost:/,"",array[x])
swap(array,2,x)
}
if (array[x] ~ /[pP]ort:.*/) {
gsub(/[pP]ort:/,"",array[x])
swap(array,3,x)
}
}
kill(array,3,(params + 1))
parray(array,3)
print a = _elems(array), "in array."
print "IN#############################IN"
for (all in array) {
print array[all]
}
(The hash marks are part of the sourced file and program output::sorry for the mess.)
Here is the output:
###################OUT:
################
##############
mode:udp
############
host:localhost
###########
port:23-900
###########
::Note->parray() works here.
###################OUT:
::Note->parray() does not work here(?)
3 in array.
::Note->kill() has eliminated the specified elements.
::Should only be three elements now.
IN######################IN
############
###########
###########
udp
localhost
23-900
::note->What(?) That's 10 elements again.
My understanding and practical experience is that the awk
array is not some "protected object" and can be passed
and changed in function calls. What am I missing?
Don't ask what the program does. It is a proof of
concept in gawk 3.1 and is evil.