First, don't pre-define. Let Perl do the auto-vivification for you. That's what it's there for. Especially because you don't know for certain what will be there, just what structure it will be in.

Secondly, you want to do something along the lines of:

# Caveat Lector - this is untested! foreach $tag (@tags) { push @{$tagstack{$tag->name}}, { text => $tag->value, attributes => { split /[\s=]+/, $tag->attributes }, }; }
Obviously, I'm assuming that $tag is some object with three methods - name(), value(), and attributes(). These will have to return the appropriate values from the data source. Also, I'm assuming that attributes() will return everything within the tag definition other than the tag's name. If all you get is the text, you could do something like:
foreach my $tag (@tags) { my ($name, $attributes, $value) = $tag =~ m#^<(\w+)\s+(\w+)?\s*>(.*?)</\1>$#; # Use $name, $attributes, and $value as per above }
The regex could be tightened, but this is off-the-cuff.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: Predefining complex data structures? by dragonchild
in thread Predefining complex data structures? by Ionizor

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.