Re: Having Trouble Recursing a Function
From: Jim Gibson (jgibson_at_mail.arc.nasa.gov)
Date: 02/25/05
- Next message: Jim Keenan: "Re: glob"
- Previous message: A. Sinan Unur: "Re: Division/math bug in perl?"
- In reply to: Mark Healey: "Having Trouble Recursing a Function"
- Next in thread: Tad McClellan: "Re: Having Trouble Recursing a Function"
- Reply: Tad McClellan: "Re: Having Trouble Recursing a Function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Feb 2005 15:39:13 -0800
In article <pan.2005.02.24.22.40.43.424469@spammer.die>, Mark Healey
<die@spammer.die> wrote:
> Can anyone tell me why the following only goes one level deep in the
> directory tree?
>
> I'm stumped
>
>
> #!/usr/bin/perl
>
> use strict;
> use Cwd;
>
> my $cdir;
> my @files;
>
> $cdir = getcwd();
>
> doDir($cdir);
>
> foreach(@files)
> {
> printf("$_\n");
> }
>
> exit;
>
> sub doDir
> {
> my $dir = $_[0];
> printf("####$dir####\n");
> my $fname;
> opendir(DIRHANDLE, $dir);
> my @list = readdir(DIRHANDLE);
> closedir(DIRHANDLE);
> foreach(@list)
> {
> chomp;
> if(-d $_)
This test is checking the existence of a subdirectory in the current
directory. When you are checking the sub-subdirectories in a
subdirectory, this check will fail, because it is looking for that
directory in the current directory, and it doesn't exist there. You
need to check "$dir/$_" instead.
> {
> unless(/\.\.?\z/)
> {
> $fname=$dir.'/'.$_;
> doDir($fname);
> }
> }
> else
> {
> if(/\.mp3\z/)
> {
> $fname=$dir.'/'.$_;
> push(@files, $fname);
> }
> }
> }# end foreach(@list)
> }# end doDir()
Better still would be to use File::Find, a standard module.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
- Next message: Jim Keenan: "Re: glob"
- Previous message: A. Sinan Unur: "Re: Division/math bug in perl?"
- In reply to: Mark Healey: "Having Trouble Recursing a Function"
- Next in thread: Tad McClellan: "Re: Having Trouble Recursing a Function"
- Reply: Tad McClellan: "Re: Having Trouble Recursing a Function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]