AWK Problem in recursive function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Problem in recursive function
# 1  
Old 06-14-2010
AWK Problem in recursive function

Hi, I have a file like this

Code:
SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo
SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos
SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc
SPF_HC00006|iCalcular_Articulo_167_Anterior|--->|SPF_HC00031|pstcObtener_Por_CB_Inc
SPF_HC00007|iCalcular_Reduccion_Cesantia|--->|SPF_HC00023|iCalcular_Edad
SPF_HC00007|iCalcular_Reduccion_Cesantia|--->|SPF_HC00030|pstcObtener_Porc_Cesantia
SPF_HC00008|iCalcular_Ayudas|--->|SPF_HC00026|pstcObtener_Parametro
SPF_HC00008|iCalcular_Ayudas|--->|SPF_HC00026|pstcObtener_Parametro
SPF_HC00009|iCalcular_Asignaciones|--->|SPF_HC00025|pstcObtener_Porc_Bene_Parent
SPF_HC00010|iCalcular_Importe_Mensual_Tope|--->|SPF_HC00033|pstcObtener_Por_Reduccion_Art_Ant

Columns 1 and 2 are the codename and the verbose name for a function that is calling to another function (defined by columns 4 and 5, in the same way)

I have the folowing script to process the file

Code:
#Este AWK imprime el arbol de llamadas de funciones

BEGIN{
    FS="|"
    Debug=1
}
{
    t=FuncIsUsing[$1] $4 ", "
    FuncIsUsing[$1]=t
    
    t=FuncIsUsedBy[$4] $1 ", "
    FuncIsUsedBy[$4]=t
    
    TotFunctions[$1]
    TotFunctions[$4]
}
END{
    #Detect not used functions
    for (x in TotFunctions){
        if (!(x in FuncIsUsedBy)){
            print "Function ["x"] is not used"
            FuncNotUsed[x]
        }
    }
    
    #Recorrer cada rama de las que no son llamadas
    for (x in FuncNotUsed){
        printBranch(x, 1)
    }
}
function printBranch(raiz, sangria){
    for (i=1;i<sangria;i++){
        printf "    "
    }
    print "["raiz"]"
    if (raiz in FuncIsUsing){
        if (1==Debug) print "Function ["raiz"] is using: ["FuncIsUsing[raiz]"]"
        split(FuncIsUsing[raiz], funcQueLlama, ", ")
        
        for (f in funcQueLlama){
            if ("" != funcQueLlama[f])
                printBranch(funcQueLlama[f], sangria+1)
        }
    }else{
        if (1==Debug) print "Is not using functions"
    }
}

It produces the following output

Code:
Function [SPF_HC00041] is not used
Function [SPF_HC00045] is not used
Function [SPF_HC00046] is not used
Function [SPF_HC00048] is not used
Function [SPF_HC00049] is not used
Function [SPF_RS00045] is not used
Function [SPF_RS00063] is not used
Function [SPF_RS00064] is not used
Function [SPF_HC00035] is not used
Function [SPF_HC00050] is not used
[SPF_HC00035]
Function [SPF_HC00035] is using: [SPF_HC00034, SPF_HC00044, SPF_HC00044, ]
    [SPF_HC00034]
Function [SPF_HC00034] is using: [SPF_HC00047, ]
        [SPF_HC00047]
Is not using functions
[SPF_HC00045]
Function [SPF_HC00045] is using: [SPF_HC00039, SPF_HC00024, ]
    [SPF_HC00039]
Is not using functions
    [SPF_HC00024]
Is not using functions
[SPF_RS00063]
Function [SPF_RS00063] is using: [SPF_HC00014, SPF_HC00001, ]
    [SPF_HC00014]
Function [SPF_HC00014] is using: [SPF_HC00026, ]
        [SPF_HC00026]
Is not using functions
[SPF_RS00045]
Function [SPF_RS00045] is using: [SPF_HC00013, SPF_HC00026, ]
    [SPF_HC00013]
Function [SPF_HC00013] is using: [SPF_HC00029, ]
        [SPF_HC00029]
Is not using functions
[SPF_HC00046]

The matter is in the next output part:

Code:
Function [SPF_RS00063] is using: [SPF_HC00014, SPF_HC00001, ]
    [SPF_HC00014]
Function [SPF_HC00014] is using: [SPF_HC00026, ]
        [SPF_HC00026]
Is not using functions
[SPF_RS00045]

It dosn't print detail for SPF_HC00001, when it makes the recursive call, lost the current values for my array funcQueLlama.

Any suggestion for dont lose this values?
# 2  
Old 06-14-2010
declare ALL your local variables in the function declaration - even-though you don't pass them. See if it helps:
Code:
function printBranch(raiz, sangria,    i,funcQueLlama,f){
    for (i=1;i<sangria;i++){
        printf "    "
    }
    print "["raiz"]"
    if (raiz in FuncIsUsing){
        if (1==Debug) print "Function ["raiz"] is using: ["FuncIsUsing[raiz]"]"
        split(FuncIsUsing[raiz], funcQueLlama, ", ")
        
        for (f in funcQueLlama){
            if ("" != funcQueLlama[f])
                printBranch(funcQueLlama[f], sangria+1)
        }
    }else{
        if (1==Debug) print "Is not using functions"
    }
}

# 3  
Old 06-14-2010
Thx a lot vgersh99, I'm checking the output, but it looks good.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

recursive function

Hello forum members, Please wirte a sample program for print the 1 - 100 and 100 -1 using recursive function. Thanks & regards Siva Rangnanath (2 Replies)
Discussion started by: workforsiva
2 Replies

2. Shell Programming and Scripting

Run recursive function with variables over SSH

Hi, I don't know if you can help or if this is even possible, but I am trying to run the following function over an ssh and (depending on the itteration I choose) keep getting unexpected token or undefined symbol errors. The function is: killtree() { typeset parent=$1 typeset child... (1 Reply)
Discussion started by: RECrerar
1 Replies

3. Shell Programming and Scripting

Problem using function in awk

I created two functions that output two random variables. I want to output them in the output file. But it does not seem to work. # Function rgaussian1(r1, r2) # Gaussian random number generator function rgaussian1(r1, r2) { pi = 3.142 v1 = sqrt( -2 * log(rand()) ) v2... (18 Replies)
Discussion started by: kristinu
18 Replies

4. Shell Programming and Scripting

perl recursive function issue

I am facing some problem in perl recurssive function function my @array_parent = (Some inegers); my $outputfile = 'output.txt'; my $master_file = 'master.txt'; open (MASTER,"$>>master.txt"); foreach my $child (@array_parent){ my $line = `grep "$child" "$outputfile"`; ... (1 Reply)
Discussion started by: pritish.sas
1 Replies

5. Shell Programming and Scripting

Recursive function and arrays

I have the following function in a bash script that fails to return the sorted array. I think the problem lies in the recursion not correctly passing the arrays, but I can't tell what I'm doing wrong. Anyone see the problem? function quicksort () { local array=( `echo "$1"` ) local... (7 Replies)
Discussion started by: tkg
7 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

Recursive function in unix

Can someone tell me, how do i write a recursive code using shell ( eg like 'for' loop in C) which outputs the record to a database table as one row per iteration? (7 Replies)
Discussion started by: goutam_igate
7 Replies

8. Shell Programming and Scripting

Recursive function call problem

This is shell script I have made to lists out directory contents and filenames for any given directory (without using ls command). There is some problem in dirfunc function call which I have marked 1 is not working. Can anybody suggest what is the problem there and how should I correct it. ... (2 Replies)
Discussion started by: netresearch
2 Replies

9. Shell Programming and Scripting

Problem with Recursive function

Hi all, I have to move all the files in a tree directory structure to a single directory. Inorder to know which file is from which directory , i'll have to add the name of the directory to the file name. For this i wrote a recursive function which is as follows... (4 Replies)
Discussion started by: malle
4 Replies

10. Programming

recursive function

Hi everyone, i need your input on this. We can express a function recursivly like this A(n) = (2 n = 0 5 n = 1 A(n − 1) + A(n − 2) % 47 n > 1 How would i go about constructing a recursive function for this?... (1 Reply)
Discussion started by: bebop1111116
1 Replies
Login or Register to Ask a Question