Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: recursive path function falls into infinite loop

by tachyon (Chancellor)
on Nov 25, 2004 at 07:05 UTC ( #410341=note: print w/replies, xml ) Need Help??

in reply to recursive path function falls into infinite loop

First the obligarory use File::Find. Anyway here is how to do it without using recursion - it is presented as an example rather than a recommendation. Search is width first rather than depth first. It is vaguely interesting in that we add new dirs to the array we are currently looping over. This comes in handy for other things as well.

#!/usr/bin/perl -w use strict; my $root = "c:\\cl"; my $delim = $^O =~ m/Win32/ ? "\\" : "/"; $root .= $delim unless $root =~ m!\Q$delim\E$!; # ensure we have trail +ing slash my @dirs = ($root); my @files; for my $path (@dirs){ opendir ( DIR, $path ) or next; # skip dirs we can't r +ead while (my $file = readdir DIR) { next if $file eq '.' or $file eq '..'; # skip the dot files next if -l $path.$file; # skip symbolic links if ( -d $path.$file ) { push @dirs, $path.$file.$delim; # add the dir to dir l +ist } else { push @files, $path.$file; # add the file to file + list } } closedir DIR; } do{ local $"=$/; print "Directory list\n@dirs\n\nFile List\n@files\n" +};



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410341]
[ambrus]: Corion: does building airports "somewhat outside of" a city ever work? cities always grow around the airport quickly, because it's practical to be close to it, and after that, people start to complain that the airplanes land so close to their house and sh
[ambrus]: ould keep down the noise

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-02-23 12:08 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (346 votes). Check out past polls.