Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How do I get directory structure into a hash?

( #219919=categorized question: print w/ replies, xml ) Need Help??
Contributed by filmo on Dec 15, 2002 at 00:07 UTC
Q&A  > directories


Description:

I've looked at File::Recurse and File::Find, but they both don't seem to handle sub-directories as I'd like. What I'm looking for is a nested hash of the directory, its files and sub-directories. i.e.
%result = ('top' =>[ file1, file2, file3, subdir1 => [sub_file1, sub_file2], subdir2 => [sub_file3, sub_subDIR =>[sub_sub_file1]], etc... ]);
If the top directory is "home" and one of the subdirectories is 'user1', I would like be able to access the files as
$result{'home'}[0] $result{'home'}{'user1'}[0] $result{'home'}{'user1'}[1], etc

Answer: How do I get directory structure into a hash?
contributed by Aristotle

Tie::Dir

If you build your own, to avoid the problem merlyn you would probably do something like

$result{home}{"."}[0] $result{home}{user1} $result{home}{user1}{"."}[0]
A starting point:
#!/usr/bin/perl -w use strict; use File::Find; use File::Spec::Functions qw(splitdir); use Data::Dumper; my %hash; find( sub { return if $_ eq '.' or $_ eq '..'; my $cursor = \%hash; $cursor = $cursor->{$_} ||= {} for splitdir $File::Find::dir; -d $_ ? $cursor->{$_} = { "." => [] } : push @{$cursor->{"."}}, $_; }, @ARGV); print Dumper \%hash;
Answer: How do I get directory structure into a hash?
contributed by BrowserUk

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;
Answer: How do I get directory structure into a hash?
contributed by merlyn

$result{'home'}[0] $result{'home'}{'user1'}[0]
That's never gonna work. $result{'home'} cannot be both a hashref and an arrayref.
Answer: How do I get directory structure into a hash?
contributed by filmo

Okay, so my structure is a bit messed up. :O
So how do I get:

$result{'home'}{'file1'} $result{'home'}{'user1'}{'file2'} $result{'home'}{'user1'}{'file3'} $result{'home'}{'user2'}{'subdir'}{'file4'} etc..

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (10)
    As of 2014-12-28 06:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (178 votes), past polls