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

Hi,

I have a lot of paths like this in a database:

/demo/test/file1234.cgi
/demo/test/core/newfile.cgi
/admin/test/
/core/system/init.conf


And I would like to somehow take that raw data, and parse it so that I can print out an html folder hierarchy.

Ive tried this the old manual way but i was just curious if anyone knew of a module or anything that would get the same result ? In an easier manner ?
  • Comment on Dynamically creating a folder hierarchy

Replies are listed 'Best First'.
Re: Dynamically creating a folder hierarchy
by tilly (Archbishop) on Feb 22, 2009 at 21:58 UTC
    My only suggestion would be to take the dataset, strip trailing slashes, and then sort it. The sorted data will appear in the same order as an html folder hierarchy, and printing it out should be fairly easy.
Re: Dynamically creating a folder hierarchy
by BrowserUk (Patriarch) on Feb 23, 2009 at 06:47 UTC

    It's kind of unweildy, but it can be done as a "one liner". Assumes the list of paths are in junk.dat:

    perl -MList::Util=reduce -nle"$p{reduce{$p{$a}=1;$a=qq[$a/$::b]}split'/'}=1}{print for sort key +s%p" junk.dat /admin /admin/test /core /core/system /core/system/init.conf /demo /demo/test /demo/test/core /demo/test/core/newfile.cgi /demo/test/file1234.cgi

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Dynamically creating a folder hierarchy
by repellent (Priest) on Feb 23, 2009 at 01:01 UTC
Re: Dynamically creating a folder hierarchy
by DrHyde (Prior) on Feb 23, 2009 at 10:49 UTC
    You really need to show us the sort of output you want. You don't make it clear whether you want a list of filenames, or you want a fancy ASCII-art tree, for example. Second, you need to show us what you've tried so far, and explain how its output differs from what you expect and from what you want. Only then can we help you.