in reply to How to map a directory tree to a perl hash tree
Here's another way. I handle the recursion myself, without File::Find. I used your example as a model.
Is this what you want or am I missing something?
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $dirth = &dirTreeHash(shift); sub dirTreeHash { my $dir = shift; return unless (defined $dir && -d $dir); $dir =~ s#\\#/#g; # Win32 :-( my $dirth = {}; opendir(DIR, $dir) || die "Unable to opendir $dir\n"; my @files = grep {!/^\.\.?$/} readdir(DIR); closedir(DIR); map {$dirth->{$_} = (-d "$dir/$_" ? &dirTreeHash("$dir/$_") : '')} @ +files; return $dirth; } $Data::Dumper::Indent = 1; print Dumper $dirth;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to map a directory tree to a perl hash tree
by merlyn (Sage) on Mar 12, 2001 at 15:25 UTC | |
by DeaconBlues (Monk) on Mar 13, 2001 at 01:35 UTC | |
|
Re: Re: How to map a directory tree to a perl hash tree
by Rudif (Hermit) on Mar 12, 2001 at 01:44 UTC |