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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Local variables get corrupted when calling another function.

Status
Not open for further replies.

calabrra

Technical User
Sep 16, 2003
14
US
Hi,

I'm calling a function from within a function. This causes local variables to be corrupted because they happen to have the same name in the function I am calling.

Is there a workaround to this annoyance without having to rename variables?


Thanks,
-Bob
 
pls post your code.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad,

I got the code to work right by changing &quot;strpos&quot; to &quot;strcol&quot; and &quot;result&quot; to &quot;result_ascii&quot; in the calling function. Nevertheless, I would like to know if there's a cleaner way to preserve parameter values in the calling function without changing parameter names.

Thanks,
-Bob


BEGIN{printf(&quot;Output: %s\n&quot;, hex2ascii(hexnum) );}
END{}

###################################################
function hex2ascii(hex_string)
###################################################

{

#Convert any size hex string to 8-bit ASCII

result_ascii = &quot;&quot;;

strcol = length(hex_string) - 1;

while (strcol > 0)

{

#Capture binary 8-bit chunks starting from right to left
chunk = substr(hex_string, strcol, 2);

#Convert hex to decimal
dec = hex2uint(chunk);

#Convert decimal to ASCII character
digit = sprintf(&quot;%c&quot;, dec);

#Concatenate and resume looping
result_ascii = digit result_ascii;

strcol = strcol - 2;

}

return(result_ascii);

}

###################################################
function hex2uint(hex_string) {
###################################################

# Convert hex string to unsigned integer

result = 0;
power = 0;
MSD = substr (hex_string, 1, 1);

for (strpos = length(hex_string); strpos > 0; strpos--)
{
digit = substr(hex_string, strpos, 1);
if (match(digit, /[a-fA-F]/))
{
gsub(/[aA]/, &quot;10&quot;, digit);
gsub(/[bB]/, &quot;11&quot;, digit);
gsub(/[cC]/, &quot;12&quot;, digit);
gsub(/[dD]/, &quot;13&quot;, digit);
gsub(/[eE]/, &quot;14&quot;, digit);
gsub(/[fF]/, &quot;15&quot;, digit);
}
result = result + digit*(16**power);
power++;
}

return (result);

}
 
AWK has an odd way of declaring local variables - they're listed as additional parameters (which you don't pass obviously).

So it would be
Code:
function hex2uint(hex_string,
  result, power, MSD ) {

  result = 0;
  power  = 0;
  MSD = substr (hex_string, 1, 1);

  for (strpos = length(hex_string); strpos > 0; strpos--)
  {
    digit  = substr(hex_string, strpos, 1);
    if (match(digit, /[a-fA-F]/))
    {
      gsub(/[aA]/, &quot;10&quot;, digit);
      gsub(/[bB]/, &quot;11&quot;, digit);
      gsub(/[cC]/, &quot;12&quot;, digit);
      gsub(/[dD]/, &quot;13&quot;, digit);
      gsub(/[eE]/, &quot;14&quot;, digit);
      gsub(/[fF]/, &quot;15&quot;, digit);
    }
    result = result + digit*(16**power);
    power++;
  }

  return (result);

}

--
 
the convention is to declare ALL 'local' function variables in the formal declaration of a function.
If they are NOT declared in the formal declaration of the function, their scope is global.

Also, the convention is to separate the 'passed-in' paramerters from the 'local' variables with spaces:
Here's an example based on your code for the 'called' function - you probably need to do the same to the 'calling' function as well to avoid any global variables contamination:

###################################################
function hex2uint(hex_string, result, power,MSD,strpos,digit) {
###################################################

# Convert hex string to unsigned integer

result = 0;
power = 0;
MSD = substr (hex_string, 1, 1);

for (strpos = length(hex_string); strpos > 0; strpos--)
{
digit = substr(hex_string, strpos, 1);
if (match(digit, /[a-fA-F]/))
{
gsub(/[aA]/, &quot;10&quot;, digit);
gsub(/[bB]/, &quot;11&quot;, digit);
gsub(/[cC]/, &quot;12&quot;, digit);
gsub(/[dD]/, &quot;13&quot;, digit);
gsub(/[eE]/, &quot;14&quot;, digit);
gsub(/[fF]/, &quot;15&quot;, digit);
}
result = result + digit*(16**power);
power++;
}

return (result);

}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
The correct way to declare local variables in
a function is with this syntax.

Code:
function myfunction(param1, local1,local2,local3) {
 
 if (length(param1) > 20) {
     local1 = substr(param1,1,10)
     local2 = substr(param1,11,20)
     local3 = substr(param1,21,length(param1))
     return local1&quot; &quot;local2&quot; &quot;local3
 }
}

Collisions in the global namespace used by awk
are frustrating but can be avoided by declaring
the variables as local within the function itself.
 
Vlad/Salem, thanks for your timely and helpful responses.

-Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top