Sponsored Content
Top Forums Shell Programming and Scripting Local variable in functions (gawk) Post 302929908 by solaris21 on Sunday 28th of December 2014 09:18:03 PM
Old 12-28-2014
Local variable in functions (gawk)

Hi Everybody Smilie

I need your help, because i know a local variable in a function for example k, it is different of other variable(with the same name k) this a global variable. Is that right?

Code:
dgawk> run
Starting program: 
3238860128818202
3 4 7 11 12 13 17 22 23 32 35 37 41 48 49 55 63 
Stopping in Rule ...
Breakpoint 1, main() at `deco-int.awk':16
16          parseo_datos();
dgawk> s
parseo_datos() at `deco-int.awk':88
88       k = 1;
dgawk> s
89       inicio = 69;
dgawk> s
90       while (k <= 15) {
dgawk> set k=10
k = 10
dgawk> set inicio=177
inicio = 177
dgawk> p k
k = 10
dgawk> p inicio
inicio = 177
dgawk> s
91            i = campo_enc[k];
dgawk> s
92            data=0;
dgawk> p i
i = "32"
dgawk> s
93            long_data=0;
dgawk> s
94            if (C[i,4] == "F") {
dgawk> s
99            if (C[i,4] == "V") {
dgawk> s
100              long_data = strtonum(quita_ceros(cadhex_to_caddec(substr($0, inicio, C[i,5] * 2))));
dgawk> p C[32,4]
C["324"] = "V"
dgawk> p k
k = 10
dgawk> s
100              long_data = strtonum(quita_ceros(cadhex_to_caddec(substr($0, inicio, C[i,5] * 2))));
dgawk> p k
k = 10
dgawk> n
101              if (long_data <= C[i,6]) {
dgawk> p k
k = 1

the function "quita_ceros", I have it in include file:

Code:
function quita_ceros(cad)
{
 k=1;
 while (k <= length(cad)) {
      if (substr(cad , k , 1) == "0") {
         cad = substr(cad, k + 1, length(cad));
         continue;
        }
      else
          break;
      k++;
     }
 return cad;
}

if you see the debug, the value of variable global change after call the function "quita_ceros", why? both variable have the same name, but a is local variable and other is a variable global.

Thanks for advance.Smilie

Last edited by Scrutinizer; 12-29-2014 at 12:10 AM.. Reason: icode tags => code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Global variable becomes local

I have encountered a very weird behavior of a global variable in Korn Shell in AIX: A function f1 in my script pipes the output of the function f2 to a program. A variable defined as global using typeset gets its value in f2. That value is not seen in f1. If I remove the pipe ksh recognizes the... (2 Replies)
Discussion started by: odashe318
2 Replies

2. Shell Programming and Scripting

new local variable in bash

hi, i need to add a new local variable to my script. then i want to display its value along the title(with echo). desired result: password = x Actual result: x for some reason the below line displays the x value. password=cat config.xml |grep 'DB Password'|cut -f3 -d '='|cut -f2 -d... (1 Reply)
Discussion started by: meny
1 Replies

3. Shell Programming and Scripting

solved -gawk, search for pattern - mark the previous line as a variable?

Im trying to parse ifconfig with awk and setup a bunch of variables in one shot. But Im having trouble figuring out how to work with data in previous lines. ifconfig output: eth0 Link encap:Ethernet HWaddr 00:50:DA:10:7F:1B inet addr:10.10.10.10 Bcast:10.10.10.127 ... (0 Replies)
Discussion started by: trey85stang
0 Replies

4. Shell Programming and Scripting

Using local variable on a remote machine

Hi, I'm writing a korn shell script where the user enters a variable and I have to create a directory remotely which contains the name of that variable. Example. print 'Please enter variable:' read variable ssh user@host 'mkdir before_$variable;' Thank you. (4 Replies)
Discussion started by: jangozo
4 Replies

5. Shell Programming and Scripting

Accessing local variable

Hi, Would like to know the purpose and accessing of local variable as in below code snippet: a=123 ( a=321; ) echo "a = $a" #This will print 123 How to access local a variable which is assigned with value 321 ?. .. (3 Replies)
Discussion started by: IND123
3 Replies

6. Shell Programming and Scripting

Trying to learn to use functions in gawk and not getting expected output.

I've been working on improving my awk, and the next thing I want to learn is to properly use functions (I understand functions in shell and python). I have the following code which includes how I did this without functions before, and two attempts I've made to do it with functions: function... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

7. Shell Programming and Scripting

Share variable between functions

Hi, gtkdialog gui seperate buttons use seperate functions in their GUI. A variable (newFolderName) is being set in one function (funcA) and it needs to be called in another function (funcB). I thought using global variable (like in python) concept should work however it does not. ... (4 Replies)
Discussion started by: singhai.nish
4 Replies

8. Shell Programming and Scripting

Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background. Here is my example $ cat sample_program.sh #!/bin/ksh exec_func () { sleep 1 v=`expr $1 + 100` print $v } export OUT_ARR date for i in 1 2 do OUT_ARR=`exec_func $i` &... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

9. Shell Programming and Scripting

Source functions from variable

I have a functions file containing functions: file.functions: myTest () { echo "MYPWD=$(echo $(pwd))" echo "MYLOGNAME=${LOGNAME}" } myCar () { echo "MYDATE=$(date)" echo "MYUPTIME=$(uptime)" } i know i can source this file in another script using something similar to this: source... (8 Replies)
Discussion started by: SkySmart
8 Replies

10. Shell Programming and Scripting

Source functions from variable

so i found a way to do this. #!/bin/bash MYF=$(/home/jason/myfunctions) source <(printf '%s\n' "${MYF}") echo "${var1}" /home/jason/myfunctions is a script that outputs information..i.e. functions...etc that needs to be sourced. I have no control over /home/jason/myfunctions. i can... (3 Replies)
Discussion started by: SkySmart
3 Replies
Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSuUser(Contributed Perl DocumPerl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr(3)

NAME
Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr - Use 4-argument "substr" instead of writing "substr($foo, 2, 6) = $bar". AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
Conway discourages the use of "substr()" as an lvalue, instead recommending that the 4-argument version of "substr()" be used instead. substr($something, 1, 2) = $newvalue; # not ok substr($something, 1, 2, $newvalue); # ok The four-argument form of "substr()" was introduced in Perl 5.005. This policy does not report violations on code which explicitly specifies an earlier version of Perl (e.g. "use 5.004;"). CONFIGURATION
This Policy is not configurable except for the standard options. SEE ALSO
"substr" in perlfunc (or "perldoc -f substr"). "4th argument to substr" in perl5005delta AUTHOR
Graham TerMarsch <graham@howlingfrog.com> COPYRIGHT
Copyright (c) 2005-2011 Graham TerMarsch. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2014-06-09 Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr(3)
All times are GMT -4. The time now is 04:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy