Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner question : directory structure and hash tables

Status
Not open for further replies.

weirdcpu

IS-IT--Management
Mar 19, 2005
22
FR
Hello everybody,

I start to learn Perl just a few days ago and i would like to reproduce the tree structure of an hard drive into a hash list.

What i think is something like this :

rep1 ---fic1 which will give : { "rep1" => { "fic1",
fic2 "fic2", "srep11" => {"fic3"}},
srep11 "rep2" => {"fic4",
fic3 "srep21" => {"fic4}}}
rep2 ---fic4
srep21
fic4
Is there a module which can help me to obtain this ?

I will use the hash table first to transform some of the file names and after to produce some basic html page with a
<table> fill with the content of the hash table.

Thanks for your suggestions
 
There is no module I know of that will build a hash from a directory listing automatically.

You can use File::Find to recure directories and do what you want with the data, such as build a hash of hashes. You can use the preprocess option to sort the files in a directory or do other things to the files.

It would also be easy to wrap it all up in html for display in a web browser.



 
This is a very simple example of a script using File::Find that opens a directory, recurses the directory, and displays the files (sorted by size) in a browser:

Code:
#!perl -w
use strict;
use File::Find;
use CGI qw/:standard/;
print header;
my $dir = param('dir') || './';
my @files = ();
print '<table><tr bgcolor="#EEEEEE"><td>Filename</td><td>Size</td></tr>';
find({wanted => \&display_em, preprocess => \&sort_em}, $dir);
print '</table>';

#sorts files in directories: largest to smallest files
sub sort_em {
   @files = @_;
   @files = sort {-s $b <=> -s $a} @files;
   return @files;
}

# displays the sorted lists from sub sort_em
sub display_em {
   print "<tr><td>$_</td><td>" . (-s $_) . "</td></tr>\n" if (-f);
}

Read the File::Find documents for more details on using the module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top