First off you cant "predefine" your data structure in Perl. There is no means by which you can explicitly specify your data structure. Instead Perl provides for easy ways to implicitly define your data structure. As well as interact with it and redefine it on the fly.

So how to do some of the things you want to do...

my %struct; foreach my $elem (@elements) { # loop over all the elements # check to make sure our hash key has an array we can push onto. $struct{$elem->name}=[] unless $struct{$elem->name}; # now create a new sub hash to push onto the array later my %hash=(text=>$elem->text, attributes=>[]); #initialize it # loop over each attribute in the element foreach my $attrib ($elem->attribs) { # push the elements onto the attributes array push @{$hash{attributes}},$attrib->name,$attrib->value; } # push a reference to the newly created hash on the array stored for + this element type. push @{$struct{$elem->name}},\%hash; }
Now of course you will have to figure out how to convert the pseudo methods ive used here into the real thing. Also, iirc XML does not allow for two attributes of the same name in one tag, so instead of using an array to store them just use a hash (unless of order is important).

HTH

UPDATE The line where I put an array in explicitly is not needed in this scenario, but if we code like

push @{$hash{$key}},'var' unless @{$hash{$key}}>5;
we would, because autovivification doesnt happen in that context. Sorry. And just now in the CB chip pointed out that changing the condition to
push @{$hash{$key}},'var' unless @{$hash{$key}||[]}>5;
would also do the trick, and is probably more elegant, if a touch obfu'd. Thanks chip.

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.


In reply to Re: Predefining complex data structures? by demerphq
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.