ashok13123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am having a number of files in a hierarchial order. Something like It is not like one file inside another, only filenames will be there... Say inside Main, three filenames will be there,sub1.sub2,sub3..........
Main --------------------------------------------------------- sub1 sub2 sub3 sub4 -------------------------------------------------------- sub11 sub21 sub31 sub41 sub12 sub22 sub32 sub42 sub13 sub23 sub33 sub43 ___________________-____________________________________
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 open
Main sub1 sub11 sub12 sub13
then again go back to Main
sub2 sub21 sub22 sub23
then again go back to Main
sub3 sub31 sub32 sub33
etc............. How can I do it with recursion???????????? i tried it like
#! /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"; }
I am inside the infinite loop...How can i print it correctly. Thanks in advance

Replies are listed 'Best First'.
Re: Recursive function in perl
by targetsmart (Curate) on Jun 18, 2009 at 08:42 UTC
    I am having a number of links in a hierarchial order
    IMO if you have it in a Hash of hashes (see perldsc), then the task becomes easy, just walk through the keys and respective values and call appropriate functions, maintain a array and push the values that you are walking through from particular key to print the depth(full link path).

    How can I do it with recursion?
    Why do you want to specifically go for recursion??, can't you achieve using simple ways unless you want to do something very purposeful inside that depth of link.


    Vivek
    -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
Re: Recursive function in perl
by rovf (Priest) on Jun 18, 2009 at 09:10 UTC

    Maybe you should show some code which you already have. This makes it easier to discuss what you want to achieve. Also, you should describe how the data is represented (for instance, as nested arrays, or as a hierarchy of nested hashes etc.).

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Recursive function in perl
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 12:04 UTC

    I'm sorry, but I don't understand what you are trying to achieve. Are the files stored in nested directories? What are you trying to print, you say that you want to print link names, then you are saying that you want to open those files, and your code tries to print file contents, not names.

    Please, explain what you want to do, and I'll do my best to help you. Perhaps write some simple code that does what you want to do only for depth = 2, try it, and if it works, post it, and we'll figure out a way to change it so it works for a variable depth value.

    Regards,
    Luke

      Sorry for the confusion....
      Inside the main, there will be some filenames.....

      I want to get the filenames first....Then open each file...Inside each file there will be some other filename....then i will take these file name and open those filenames...So it will continue until the depth we are specifying....

      When the depth is achieved, it should print the file contents....from the main file to the depth

        Thank you for the clarification. I hope this gets you started.

        #!/usr/bin/perl -w use strict; my $initial_file = 'Main'; my $ultimate_level = 2; sub jak_daxter_recursor_legacy { my ($file, $level) = @_; open (FH, $file) or die "Cannot open $file: $!"; my $contents = <FH>; my @filestoprocess = split (/,/, $contents); for my $file (@filestoprocess) { print "$file\n"; jak_daxter_recursor_legacy ($file, $level+1) unless ($level == $ultimate_level); } } jak_daxter_recursor_legacy ($initial_file, 1);

        I tested it using three files:
        Main, containing 'sub1,sub2'
        sub1, containing 'sub12,sub13,sub17'
        sub2, containing 'sub21,sub22'

        The output is as follows:

        sub1 sub12 sub13 sub17 sub2 sub21 sub22

        Hope it helps. If you have any questions, please ask.

        Regards,
        Luke

Re: Recursive function in perl
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 13:19 UTC

    I'm pasting over my reply from your other node as it was reaped. In the future, consider waiting a bit instead of creating another node with the same content. Patience is a virtue, especially in the Monastery ;]

    I hope this gets you started.

    #!/usr/bin/perl -w use strict; my $initial_file = 'Main'; my $ultimate_level = 2; sub jak_daxter_recursor_legacy { my ($file, $level) = @_; open (FH, $file) or die "Cannot open $file: $!"; my $contents = <FH>; my @filestoprocess = split (/,/, $contents); for my $file (@filestoprocess) { print "$file\n"; jak_daxter_recursor_legacy ($file, $level+1) unless ($level == $ultimate_level); } } jak_daxter_recursor_legacy ($initial_file, 1);

    I tested it using three files:
    Main, containing 'sub1,sub2'
    sub1, containing 'sub12,sub13,sub17'
    sub2, containing 'sub21,sub22'

    The output is as follows:

    sub1 sub12 sub13 sub17 sub2 sub21 sub22

    Hope it helps. If you have any questions, please ask.

    Regards,
    Luke