Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How do I get directory structure into a hash?

by BrowserUk (Patriarch)
on Dec 15, 2002 at 07:58 UTC ( [id://219976]=note: print w/replies, xml ) Need Help??


in reply to How do I get directory structure into a hash?

To use the test program, supply a path to the script on the command line, but choose a smallish one because Data::Dumper uses prodigious amounts of memory to dump the structure.

The hashdir() function should be portable as far as it goes, but it doesn't check for things like symbolic links, devices etc...

#! perl -slw use strict; use Data::Dumper; sub hashdir{ my $dir = shift; opendir my $dh, $dir or die $!; my $tree = {}->{$dir} = {}; while( my $file = readdir($dh) ) { next if $file =~ m[^\.{1,2}$]; my $path = $dir .'/' . $file; $tree->{$file} = hashdir($path), next if -d $path; push @{$tree->{'.'}}, $file; } return $tree; } my $tree = hashdir( shift ); print Dumper $tree;

Log In?
Username:
Password:

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

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

    No recent polls found