{} is an empty hash reference. I could have started with an empty hash, which might have been a little easier to understand, and I should have annotated my code. So I'll do that here:
# Read in my list of paths and cut off the newlines my @list_of_paths = <DATA>; chomp @list_of_paths; my %hashed_path = (); for my $path_string (@list_of_paths) { # This tracks our descent in the path tree. We start at the top my $hpref = \%hashed_path; # Split the path into its component directories, and walk through th +em for my $dir (split m:/:, $path_string) { # At each level, if we've never been there before, create an entry + for it $hpref->{$dir} = {} unless exists $hpref->{$dir}; # Then descend into it so we're ready to insert the next level $hpref = $hpref->{$dir}; } } # See what the structure looks like use Data::Dumper; print Dumper \%hashed_path; __DATA__ /top/middle/bottom /top/middle/newleaf /top/new_middle/bottom
Output is
$VAR1 = { '' => { 'top' => { 'middle' => { 'newleaf' => {}, 'bottom' => {} }, 'new_middle' => { 'bottom' => {} } } } };
The topmost level has only the key of empty string because that's the first value in a split on '/' when the string starts with '/'.

Caution: Contents may have been coded under pressure.

In reply to Re^5: Dynamic population of a hash by Roy Johnson
in thread Dynamic population of a hash by JFarr

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.