ashok13123 has asked for the wisdom of the Perl Monks concerning the following question:
That is main link is Main Under main-> sub1,sub2,sub3,sub4 Under sub1->sub11,sub12,sub13,sub14 Under sub2->sub21,sub22,sub23,sub24 Like this i can have any number of links under sub11 also. I wanted to write a program which will print the link names depending on the level we are specifying using recursion. Say if i am specifying the depth as 3,it shold openMain --------------------------------------------------------- sub1 sub2 sub3 sub4 -------------------------------------------------------- sub11 sub21 sub31 sub41 sub12 sub22 sub32 sub42 sub13 sub23 sub33 sub43 ___________________-____________________________________
then again go back to MainMain sub1 sub11 sub12 sub13
then again go back to Mainsub2 sub21 sub22 sub23
etc............. How can I do it with recursion???????????? i tried it likesub3 sub31 sub32 sub33
I am inside the infinite loop...How can i print it correctly. Thanks in advance#! /usr/bin/perl @listofLinks=qw(Main) &fileview sub fileview { @listLinks=@_; #print @listLinks; foreach $val (@listLinks) { if ($count<2) { $count++; @newlinks=getlinks($val); &fileview(@newlinks); } &printFile($val); } } $count--; last if($count==0); } sub getlinks { $file=$_[0]; open (FH,$file); @arr=<FH>; return(@arr); } sub printFile { open(FH,"$_[0]"); @arr=<FH>; print "@arr\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Recursive function in perl
by targetsmart (Curate) on Jun 18, 2009 at 08:42 UTC | |
|
Re: Recursive function in perl
by rovf (Priest) on Jun 18, 2009 at 09:10 UTC | |
|
Re: Recursive function in perl
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 12:04 UTC | |
by ashok13123 (Novice) on Jun 19, 2009 at 12:19 UTC | |
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 12:35 UTC | |
|
Re: Recursive function in perl
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 13:19 UTC |