Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Some more details: in this post someone asked how to "take strings like:"

/file.txt /a/file.txt /a/b/c /a/b/c/file.txt /z/m/w/file.txt

and "produce something like:"

%dir_hash( 'file.txt' => '', 'a' => { 'file.txt' => '', 'b' => { 'c' => { 'file.txt'

In my reply I pointed out that there should be a module designed for exactly this kind of things, which I still fail to remember. (Hey, anyone here? Update: It's Data::Diver, thanks to tye!) But first of all I provided a minimal example to accomplish not exactly the same task, but a very close one; the original code is as follows:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %dirhash; while (<DATA>) { chomp; my $last=\%dirhash; $last=$last->{$_} ||= {} for split qr|/|; } print Dumper \%dirhash; __END__ /file.txt /a/file.txt /a/b/c /a/b/c/file.txt /z/m/w/file.txt

In it I have to compensate for perl not autovivifying in this case. Then Brian McCauley provided an alternative way, precisely suggesting that one do

my $last=\\%dirhash; $last=\$$last->{$_} for split qr|/|;

Instead, which is the Autovivification trick I'm "advertising" here.

In reply to Re: Autovivification trick by blazar
in thread Autovivification trick by blazar

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2018-06-22 04:47 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (121 votes). Check out past polls.