So how to do some of the things you want to do...
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).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; }
HTH
UPDATE The line where I put an array in explicitly is not needed in this scenario, but if we code like
we would, because autovivification doesnt happen in that context. Sorry. And just now in the CB chip pointed out that changing the condition topush @{$hash{$key}},'var' unless @{$hash{$key}}>5;
would also do the trick, and is probably more elegant, if a touch obfu'd. Thanks chip.push @{$hash{$key}},'var' unless @{$hash{$key}||[]}>5;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |