perl recursive function issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl recursive function issue
# 1  
Old 06-15-2010
Java perl recursive function issue

I am facing some problem in perl recurssive function function

Code:
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"`;
        chomp($line);
        chomp($child);
        my @match = grep (/$child/i, @array_parent_level1);
        if ($child == 22105201 ){
              Lookup($line,*MASTER,$outputfile);
        }
}
close(OUT);
close(MASTER);
sub Lookup {
        my ($line,$MASTER,$outputfile)= @_;
        my ($parent,$len);
        open (OUT,"<$outputfile") or die "Can't OPen File $outputfile";
        chomp($line);
        $parent = (split /,/,$line)[8];
        print "$parent\n";
        $len = length($parent);
        print "$len\n";
        if ($len == 0) {
                print "Please goto foreach\n";
                return 1;
        }
        else{
                while(<OUT>) {
                        chomp;
                        my $field = (split/,/)[6];
                        if ($field == $parent){
                                my $line = $_;
                                print "ok\n";
                                #print "$parent|$field|$line\n";
                                sleep(2);
                                Lookup($line,*MASTER,$outputfile);
                        }
                }
         }
}

After 3 time of Lookup function call, i am getting 0 is $len variable, it should be satisfied the condition and return 1 should execute, but function is calling infinite time.

Please do the needful expert

Last edited by Franklin52; 06-15-2010 at 01:07 PM.. Reason: Please use code tags
# 2  
Old 06-15-2010
Hi can you explain in short what you are trying to do & sample input & output ??
btw you are calling a the same subroutine from same subroutine are you sure that is right ??

Quote:
Originally Posted by pritish.sas
I am facing some problem in perl recurssive function function

Code:
my @array_parent = (Some inegers);
my $outputfile = 'output.txt';
my $master_file = 'master.txt';
open (MASTER,"$>>master.txt"); # where exactly you are using this file handler ?? you opened the file in append mode but never actually writing on file handler 
foreach my $child (@array_parent){
        chomp ( my $line = `grep "$child" "$outputfile"` ) ;
        # chomp($line);
        chomp($child);
        my @match = grep (/$child/i, @array_parent_level1); # where is this array defined ??
        if ($child == 22105201 ){
              Lookup($line,*MASTER,$outputfile);
        }
}
close(OUT);
close(MASTER);

sub Lookup {
        my ($line,$MASTER,$outputfile)= @_;
        my ($parent,$len);
        open (OUT,"<$outputfile") or die "Can't OPen File $outputfile"; # Read the file in an array before sending it to function and pass the reference of array here .. so even if you call this function 100 times or 1 time you will do file I/O only once 
  
        chomp($line); # you can avoid this you are already chomping the var before sending to subroutine 
        $parent = (split /,/,$line)[8];
        print "$parent\n";
        $len = length($parent);
        print "$len\n";
        # if ( $parent )  will be sufficient to check if its empty or not no need to check its length.
        if ($len == 0) {
                print "Please goto foreach\n";
                return 1;
        }
        else{
                while(<OUT>) {
                        chomp;
                        my $field = (split/,/)[6];
                        # if $field & $parent are string then user "eq" for comparison 
                        if ($field == $parent){
                                my $line = $_;
                                print "ok\n";
                                #print "$parent|$field|$line\n";
                                sleep(2);
                                Lookup($line,*MASTER,$outputfile); # once code is here then this will be infinite loop :) 
                        }
                }
         }
}

After 3 time of Lookup function call, i am getting 0 is $len variable, it should be satisfied the condition and return 1 should execute, but function is calling infinite time.

Please do the needful expert
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

AWK Problem in recursive function

Hi, I have a file like this 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... (2 Replies)
Discussion started by: kcoder24
2 Replies

4. 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

5. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

6. 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

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

perl + Net::FTP::Recursive

Problem: It will not advance to the next user in the list. It always dies right after it sends the 2/2 files from the first users dir. $USERLIST="/export/home/mxdooley/perl_ftp/userlist"; $USER_DIR="/export/home/mxdooley/perl_ftp/homes";... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question