Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

organizing directory path data

by djw (Vicar)
on Feb 03, 2003 at 17:17 UTC ( [id://232313]=perlquestion: print w/replies, xml ) Need Help??

djw has asked for the wisdom of the Perl Monks concerning the following question:

I have directory information stored in a database that I need to build a report on. Each directory is stored with the complete path (/root/foo/bar/lastdir) and there is a lot of duplication because of this. I would like to be able to sort this data and print it out in a simple easy to read structure (besides the complete path for every item).
Here is the some sample data:
0555-DDBB/AirPhotos 
0555-DDBB/AirPhotos/AP Labels 
0555-DDBB/AirPhotos/new database 
0555-DDBB/FOO M 
0555-DDBB/FOO M/0555-055-01 Site/Photos 
0555-DDBB/FOO M/0555-055-01 Site/Photos/Digital 
0555-DDBB/FOO M/0555-055-01 Site/Photos/PhotoList 
0555-DDBB/FOO M/0555-055-01 Site/Reports/Summary 
0555-DDBB/FOO M/0555-055-01 Site/Reports/Daily 
0555-DDBB/FOO M/0555-055-02 Design 
0555-DDBB/FOO M/0555-055-02 Design/photo 
0555-DDBB/FOO M/0555-055-02 Design/photo/Ortho 
0555-DDBB/FOO M/0555-055-02 Design/Correspondance 
0555-DDBB/FOO M/0555-055-02 Design/Drafting 
0555-DDBB/FOO M/0555-055-02 Design/Drafting/DWG
I would like to be able to print out a directory structure something like:
0555-DDBB
  AirPhotos
    AP Labels
    new database
  FOO M
    0555-055-01 Site
      Photos
        Digital
        PhotoList
      Reports
        Summary
        Daily
    0555-055-02 Design
      photo
        Ortho
      Correspondance
      Drafting
        DWG
Basically remove the duplication, and some indenting to show levels ofr the directory tree. But I'm having trouble thinking of a way to store it so I can print it out. I played with some nested hashes, but it seemed to get ugly after I had 6 or 7 levels of directories to sort ($paths{$dir1}{$subdir1} $paths{$dir1}{$subdir2} etc etc). I have each directory in an array and I can split on '/', but from there I'm lost.
@dirs = ( # list from above ); foreach my $dir (@dirs) { my @singleDirs = split(/\//, $dir); # stuck }
How can I store all the info (direcotry names and location) and then print out as above? I'm just stuck for an idea - can anyone stear me in the right direction? Thanks,

djw

Replies are listed 'Best First'.
Re: organizing directory path data
by broquaint (Abbot) on Feb 03, 2003 at 17:50 UTC
    Mmm, recursion ...
    my @dirs = ( '0555-DDBB/AirPhotos', '0555-DDBB/AirPhotos/AP Labels', '0555-DDBB/AirPhotos/new database', '0555-DDBB/FOO M', '0555-DDBB/FOO M/0555-055-01 Site/Photos', '0555-DDBB/FOO M/0555-055-01 Site/Photos/Digital', '0555-DDBB/FOO M/0555-055-01 Site/Photos/PhotoList', '0555-DDBB/FOO M/0555-055-01 Site/Reports/Summary', '0555-DDBB/FOO M/0555-055-01 Site/Reports/Daily', '0555-DDBB/FOO M/0555-055-02 Design', '0555-DDBB/FOO M/0555-055-02 Design/photo', '0555-DDBB/FOO M/0555-055-02 Design/photo/Ortho', '0555-DDBB/FOO M/0555-055-02 Design/Correspondance', '0555-DDBB/FOO M/0555-055-02 Design/Drafting', '0555-DDBB/FOO M/0555-055-02 Design/Drafting/DWG', ); use strict; my %tree; for my $d (@dirs) { my $n = \%tree; $n = ($n->{$_} = exists $n->{$_} ? $$n{$_} : {}) for split('/', $d); } print_tree(\%tree, 0); sub print_tree { my($tree, $indent) = @_; for(keys %$tree) { print " " x $indent, $_, $/; print_tree($tree->{$_}, $indent + 1) if ref $tree->{$_} and keys %{$tree->{$_}} > 0; } } __output__ 0555-DDBB AirPhotos AP Labels new database FOO M 0555-055-02 Design Drafting DWG photo Ortho Correspondance 0555-055-01 Site Reports Daily Summary Photos Digital PhotoList
    So firstly we build a convenient data structure using references to references, and then we recurse through the tree we built printing out the keys. Easy as pie! Mmmm, pie ...
    HTH

    _________
    broquaint

Re: organizing directory path data
by Fletch (Bishop) on Feb 03, 2003 at 17:22 UTC
    print " " x ( 2 * @singleDirs ), $singleDirs[ -1 ], "\n";

    Update: Of course that presumes that your list of directories is already sorted and complete (e.g. if there's a Foo/Blork there's already been Foo).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found