Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Recursive search

by cdarke (Prior)
on Dec 17, 2010 at 10:36 UTC ( [id://877613]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Recursive search
in thread Recursive search

my @plf_files = {"files.plf","orphans.plf"};creates a single element array containing a reference to a hash, because you used { } (called a 'composer'). You probably meant:
my @plf_files = ('files.plf','orphans.plf');

Your subroutine tests the same thing several times, try this:
sub recursion { my $plf = shift; open my $fh, '<', $plf or die "could not open '$plf' $!"; foreach (my $line = <$fh>){ if($line =~ /(\w+\.plf)$/) { $match_plf_name = $1; next if $match_plf_name eq $plf; push @recursive_plfs ,$match_plf_name; recursion($match_plf_name); } } }
You will notice that I don't use $DATA for filehandle name. This is because there is a reserved filehandle called DATA, so $DATA would be confusing. You will also notice that there is not a grep in sight. The capturing parentheses group is saved into $1.

Finally, notice also the 'next' statement as an insurance against infinite recursion.

Replies are listed 'Best First'.
Re^4: Recursive search
by perl_mystery (Beadle) on Dec 17, 2010 at 19:39 UTC
    Thanks cdark and corion but when I open the filehandle I only see the first line in the file getting stored,confused on what is wrong?
    my $plf = shift; print "\nPLF in recursion:$plf\n"; open my $fh, '<', $plf or die "could not open '$plf' $!"; foreach (my $line = <$fh>) { print "LINE:$line"; #prints ,just the first line if($line =~ /\/(.+?\.plf)$/) { print "LINE CONDITION:$line"; #never comes here #print $1; $match_plf_name = $1; next if $match_plf_name eq $plf; push @recursive_plfs ,$match_plf_name; recursion($match_plf_name); } } }

      perl mystery:

      You forgot perldoc chomp. Alternatively, you can remove the $ from your regex.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        I did try chomp as below,didnt make a difference?

        sub recursion { my $plf = shift; print "\nPLF in recursion:$plf\n"; open my $fh, '<', $plf or die "could not open '$plf' $!"; chomp $fh; foreach (my $line = <$fh>) { chomp $line; print "LINE:$line"; if($line =~ /\/(.+?\.plf)/) { print "LINE CONDITION:$line"; #print $1; $match_plf_name = $1; next if $match_plf_name eq $plf; push @recursive_plfs ,$match_plf_name; recursion($match_plf_name); } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://877613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found