Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Is this something that is possible in Perl?

Of course.

The usual strategy to build a data structure of arbitary depth is the "create as you traverse" approach, ie you just walk level by level, create them if needed, and always keep a reference to the previous level to start the current iteration from.

Here you don't even need to do that, because you create at most one level at a time.

Since you didn't explain exactly how the resulting data structure should look like, I've added the description as an extra key called DESCRIPTION.

Without further ado, the code:

use 5.010; use strict; use warnings; my %h; my @depth; while (<DATA>){ chomp; /^(\s*)/; # 'cause it's 4 spaces here, not tabs my $level = length($1) / 4; s/^\s+//; my ($key, $val) = split /\s{2,}/, $_, 2; my $h = $level == 0 ? \%h : $depth[$level - 1]; $h->{$key}{DESCRIPTION} = $val; $depth[$level] = $h->{$key}; } use Data::Dumper; print Dumper \%h; __DATA__ Assault Assault Battery Assault and Battery Sexual Sexual Assault Deadly Assault with a deadly weapon with Assault with a deadly weapon without Assault without a deadly weapon Aggravated Aggravated Assault

Here is the output, with reduced indentation for better readability:

VAR1 = { 'Assault' => { 'Sexual' => { 'DESCRIPTION' => 'Sexual Assault' }, 'Battery' => { 'DESCRIPTION' => 'Assault and Battery' }, 'DESCRIPTION' => 'Assault', 'Aggravated' => { 'DESCRIPTION' => 'Aggravated Assault' }, 'Deadly' => { 'DESCRIPTION' => 'Assault with a deadly weapon', 'without' => { 'DESCRIPTION' => 'Assault without a deadly weapon' }, 'with' => { 'DESCRIPTION' => 'Assault with a deadly weapon' } } } };

To look up elements of arbitrary depth, you can use something like

sub lookup { my $h = \%h; $h = $h->{$_} for @_; return $h->{DESCRIPTION}; } say lookup 'Assault'; say lookup qw/Assault Deadly with/;

Which again drags a reference from the lookup of the previous level to achieve arbitrary depth.


In reply to Re: dynamic hash nesting by moritz
in thread dynamic hash nesting by karrakis

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (15)
    As of 2014-11-27 13:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (184 votes), past polls