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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to store the results of perl recursive function when applied under for loop
# 1  
Old 02-12-2010
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 i try to use this function inside a for loop passing each iteration value as argument to this function this don't work and i loose the results of every iteration . I tried to write teh o/p to a file before next iteration but it is not writing anything inside it .

Here is the code -
Code:
#!/usr/bin/perl

use warnings;

our @all_dirs;

sub list_dirs {

my $dir = shift;
#print "$dir\n";
opendir DIR, $dir or return;
 my @contents = map "$dir/$_", sort grep !/^\.\.?$/, readdir DIR;
closedir DIR;
 #print @contents;
 #sleep(20);
  
 
 foreach (@contents) 
     
 {
next unless !-l && -d;
 &list_dirs($_);

 push(@all_dirs,"$_\n");

 
 }
return @all_dirs;
 

} 

opendir MYDIR, "C:\\temp";

@all = grep !/^\.\.?$/, readdir MYDIR;



 foreach $i (@all) 
     {

@var=&list_dirs($i);
print @var;
open F1,">>C:\\$i._subdirs.txt";
foreach $e (@var) { print "$e\n";print F1 "$e\n";}
close F1;

#

}


Last edited by Scott; 02-12-2010 at 08:03 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python Paramiko multi threading to capture all output for command applied in loop

My issue : I am getting only last command output data in ouput file. Though comamnd "print(output)" displays data for all 3rd column values but the data saved in file is not what required it hs to be the same which is being printed by command"print(output)". Could you please help me to fix this,... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

Store results in table

Hello everyone, I have a shell script, which connects to the database and runs .sql file. after executing of .sql file, i need to store the results in error table. How can i achieve this one? could you please give your suggestions. here is my code. #!/bin/sh #set -vx SCHEMA_NAME=$1... (1 Reply)
Discussion started by: Rami Reddy
1 Replies

3. Shell Programming and Scripting

How to store results of ls in ftp?

Hi, I want to store the results of running ls in a directory on a remote host using ftp. For example, #!/usr/bin/ksh ftp -n hostx <<EOF user username password ls /tmp/RandomFiles.* #I need to put the results of that ls command into some file, so I can get the last file in... (3 Replies)
Discussion started by: mrskittles99
3 Replies

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

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

6. Shell Programming and Scripting

Recursive find and store

I HAVE A TEXT FILE CONTAINING THE VALUES 1.CPP 2.CPP 3.CPP 4.CPP 5.CPP 6.CPP I WANT TO TAKE EACH .CPP AND USE THE FIND COMMAND TO FIND THE LATEST VERSION OF THE FOLDER IN WHICH IT IS PRESENT. HOW DO I IMPLEMENT IT IN A WHILE LOOP I TRIED SOMETHING LIKE THIS WHILE CAT... (3 Replies)
Discussion started by: ultimatix
3 Replies

7. Shell Programming and Scripting

Grep results to store in a shell variable

I have the results of a grep with -n store in a shell variable ie VAR=`grep -n -e 'PATTERN' file` How ever I am missing the line breaks in the variable , How do I store the resualts of grep with many lines in to a variables. I want the varable should be the sawmway as we do the grep grep... (3 Replies)
Discussion started by: jojan
3 Replies

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

9. Shell Programming and Scripting

store awk results in a variable

I try to get the month (of last save) and the filename into a variable, is this possible ? something like this : for month in `ls -la | awk '{print $6}'` do if ] then a=filename of the matching file cp $a /Sep fi thanks, Steffen (1 Reply)
Discussion started by: forever_49ers
1 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question