Hi folks!

I would like to ask my fellow brothers and sisters how best to handle this situation. I want to scan a directory of files and store them in a hash in such a way as to fascilitate printing them out via nested HTML's <ol> tags. I have since figured out a way to get the results I want during printing, not data collection, but I still would like to know how to do it this way.

The structure of the directories look something like this.

  . 
  |
  |-- subdir 1
  |-+ subdir 2
  | |-- subsubdir 2.1
  | |-- subsubdir 2.2
  |-+ subdir 3
    |-+ subsubdir 3.1
      |-- subsubsubdir 3.1.1
      |-- subsubsubdir 3.1.2
      |-- subsubsubdir 3.1.3
(your standard directory tree). The problems I'm facing are twofold.

First of all, building the hash itself. I wanted to end result to be like this:

$hash{subdir 1} $hash{subdir 2}{subsubdir 2.1} $hash{subdir 2}{subsubdir 2.2} $hash{subdir 3}{subsubdir 3.1} $hash{subdir 3}{subsubdir 3.1}{subsubsubdir 3.1.1} $hash{subdir 3}{subsubdir 3.1}{subsubsubdir 3.1.2} $hash{subdir 3}{subsubdir 3.1}{subsubsubdir 3.1.3}
So I thought I could code something along the lines
@parts=split /\//,$dirname; %hash{ @parts } = (); ## this doesn't work, of course, ## just an example of the idea
where it results in something like $hash{ $parts[0] }{ $parts[1] }{ $parts[n] }=(), where { $parts[n] } would be repeated for each element of @parts. I couldn't think of a way to do this without knowing n, the index of the last element. I suppose I could do it easily enough with eval, but I try to avoid using eval as much as possible.

The second problem I have is having a hash value that can either be another hash key AND contain values. Eg, "subdir 1" can contain both files and sub directories. I know the following doesn't work:

$hash{foo}=1; $hash{foo}{bar}=2;
you get a Can't use string ("1") as a HASH ref while "strict refs" in use barf from strict. But that's the effect I want.

Hm. On second thought, an anonymous array might work:

#!/usr/bin/perl use strict; use Data::Dumper; my %hash; $hash{foo}=[undef,1]; $hash{foo}[0]{bar}=[undef,2]; $hash{foo}[0]{bar}[0]{bink}=[undef,4]; print Dumper \%hash;
In fact, that does appear to be the effect that I want... (sorry, it came to me while I was writing this). Hm. Iterating through it won't be fun, though.

So, any thoughts on any of this? Comments about the klunky anonymous array solution?

mr.nick ...


In reply to Data structure question: Directory-in-memory ? by mr.nick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.