Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pushing my limits... 2

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
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 &quot;#######################OUT:&quot;
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 = &quot;&quot;
for (x=0 ; x <= i ; x++) {
ret = length(ret) < 1 ? arr[x] &quot;\n&quot; : ret arr[x] &quot;\n&quot;
}
return ret
}

############main BEGIN() excerpted code############
info = loader(ARGV[1])
params = split(info,array,&quot;\n&quot;)
parray(array,params)
if (params) {
for (x = 1 ; x <= params + 1 ; x++) {
if (array[x] ~ /[mM]ode:.*/) {
gsub(/[mM]ode:/,&quot;&quot;,array[x])
swap(array,1,x)
}
if (array[x] ~ /[hH]ost:.*/) {
gsub(/[hH]ost:/,&quot;&quot;,array[x])
swap(array,2,x)
}
if (array[x] ~ /[pP]ort:.*/) {
gsub(/[pP]ort:/,&quot;&quot;,array[x])
swap(array,3,x)
}
}
kill(array,3,(params + 1))
parray(array,3)
print a = _elems(array), &quot;in array.&quot;
print &quot;IN#############################IN&quot;
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 &quot;protected object&quot; 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.;)
 
The swap function should read:
function swap(arr,ind,sind) {
tmp = arr[ind]
arr[ind] = arr[sind]
arr[sind] = tmp
return
}
 
Code:
#prints an number indiced array low to high.
function parray(arr,num) {
print &quot;#######################OUT:&quot;
Code:
   x = 0;
Code:
  while (x <= num) {
    x++
   print arr[x]
   }
}

The first time you call parray and use x it has the default value (0). The second time you call parray, x has keep the last value it had form the precedent call or even the last value given to x in the for loop of your main excerpt (in the if (params)).

To avoid variable name clashes from different functions you could try to name the variable from the function
Code:
#prints an number indiced array low to high.
function parray(arr,num) {
print &quot;#######################OUT:&quot;
  parray_indice = 0;
  while (parray_indice <= num) {
   parray_indice++;
   print arr[parray_indice]
   }
}

You still have to initialize the variable if you call the function more than once.
 
Thanks, I still have a hard time with this
one thing...
 
you can define local variable to a function by adding it as an extra argument in its definition.

In the following example, the variable x is local to the function :


#prints an number indiced array low to high.
function parray(arr,num ,x) {
print &quot;#######################OUT:&quot;
while (x <= num) {
x++
print arr[x]
}
}


Jean Pierre.
 
Thank you Jean Pierre.
This is a far more simple solution to solve the two problems I described (variable name clash and variable initialization).

Calling the parray function with only two actual arguments let the third formal argument x empty wich means 0 when used as a number (<=, ++).

I have to use this.
 
Vgersh told me about this originally, but I had read &quot;Effective Awk programming, about the local variable &quot;shadowing&quot; and had not paid enough attention.
So, a belated thanks to vgersh on this one too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top