Re: Hash of hashes, of hashes, of arrays of hashes



On 27 Oct 2005 17:59:48 -0700, "robic0@xxxxxxxxx" <robic0@xxxxxxxxx>
wrote:

>
>robic0@xxxxxxxxx wrote:
>> Tim O'Donovan wrote:
>> > Hi,
>> >
>> > I'm trying to create a data structure that will (eventually) make it
>> > easier to create a series of slightly different XML documents. The
>> > structure will effectively hold a template.
>> >
>> > Here's a section of code I am stuck on:
>> >
>> >
>> > #!/usr/bin/perl -w
>> >
>> > use strict;
>> > use Data::Dumper;
>> >
>> > my %xmldoc = (
>> > doc1 => {
>> > customer => [{name => 'count', format => 'int'},
>> > {name => 'unit', format => 'text'},
>> > ],
>> > order => [{name => 'expected', format => 'date'},
>> > ],
>> > },
>> > #etc
>> > );
>> >
>> >
>> > The problem I am having is determining how many keys are present in:
>> >
>> > $xmldoc{doc1}
>> >
>> > and how many elements are present in:
>> >
>> > $xmldoc{doc1}{customer}
>> >
>> > and so on.
>> >
>> > I was then planning on looping through each section and using the data
>> > to contruct the XML.
>>
>> I think the structure looks pretty tame here, but its not as simple as
>> you may think to do this. Its not a simple looping through the
>> data structures. Its itterative, is usually done with respect to
>> closures, creating a string for an inner block and substituting that
>> string for that inner block, which was an array. The next time through,
>> another block is converted, until finally you have a single string that
>> is your XML. In the event you would like to do it right, you may want
>> to check out XML:Simple... gluck!
>Just to clarify, you might drill into the arrays looking for
>a child that has no arrays (or refs to arrays). When you find one you
>create an xml string from the child array, then substitute that
>string into the parent array. So in one pass maybe you find 2 out of
>10.
>Then you repeat from the begining again. The problem comes from unamed
>(anonymous) arrays. You have to assign names (tags) to them. You also
>have to unhook the array you are replacing too via a undef that is
>ignored on subsequent passes. I'm only suggesting a possible way to do
>this, but there are modules out there if you are looking for a quick
>solution.
>>
>> >
>> > For reference, here is the dumped output of %xmldoc:
>> >
>> > $VAR1 = 'doc1';
>> > $VAR2 = {
>> > 'customer' => [
>> > {
>> > 'format' => 'int',
>> > 'name' => 'count'
>> > },
>> > {
>> > 'format' => 'text',
>> > 'name' => 'unit'
>> > }
>> > ],
>> > 'order' => [
>> > {
>> > 'format' => 'date',
>> > 'name' => 'expected'
>> > }
>> > ]
>> > };
One more clarification after pondering.
Given VAR2 above (from Dumpster), to go the other way
to xml, this would follow:
-------------------------------------
1st pass:

$VAR2 = {
'customer' => '<formatblock1>
<format>int</format><name>count</name>
</formatblock1>
<formatblock2>
<format>text</format><name>unit</name>
</formatblock2>',

'order' => '<formatblock>
<format>date</format><name>expected</name>
</formatblock>'
};

2nd pass:

$VAR2 = '<block>
<customer>
<formatblock1>
<format>int</format><name>count</name>
</formatblock1>
<formatblock2>
<format>text</format><name>unit</name>
</formatblock2>
</customer>
<order>
<formatblock>
<format>date</format><name>expected</name>
</formatblock>
</order>
</block>';
-------------------------------
This is easy to do programtically. It is itterative.
However, you can clearly see the optimizations made with
formatblock1, formatblock2 (and formatblock) above because of
the "unamed" referenced arrays. In that regard, its obvious
that using these methods and going back and forth between
reading and writing xml, and know these assumptions must be
made will not give consistent results that you can program
to. Xml tag name creation can't be fully known sometimes from
nested anonymouse arrays and hashes.
With these xml packages like XML:Simple assumptions of
naming have be made. Usually if you know the layout of the major
structural components your can roll your own xml creator.
Probably in a "fully" named structure paradigm (C) you
would have better results, not necessarily so in Perl.
Anyway, just clearing that up. Read all the docs on XML:Simple
for a better understanding going from Perl to XML.
Its always easier to go from XML to Perl, especially with SAX
and your known tags, to "ingest" data. If you do go out to
XML from perl be aware that intermediate hand modification
steps probably will be necessary.

>> >
>> > Any advice would be greatly appreciated.
>> >
>> > Thanks.
>> >
>> >
>> > Kind regards,
>> > Tim O'Donovan

.



Relevant Pages

  • Re: Put Variables giving problems
    ... strings in the format "As String * n" are fixed. ... No dynamic arrays (arrays declared with nothing inside the parentheses, ... Binary, Random, or learn XML for record ... systems, graphic file formats, and other such useful things. ...
    (microsoft.public.vb.syntax)
  • Re: Help with Array(s)
    ... I would think XML would return in something with an XML schema and you could ... Array.Sort sorts one-dimentional arrays, but don't think it will 2dim arrays ... to sort list by second int value and display. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Setting Up XML DB
    ... I just loaded your xml into a dataset and got two tables as expected. ... "Wayne Wengert" wrote in message ... > those 2 current arrays and define columns as appropriate? ... > consist of rows where the contest data is repeated in each row. ...
    (microsoft.public.dotnet.general)
  • Re: XML performance extremely slow for no obvious reason
    ... doesn't "have arrays," it sort of does, if you use a string as an array. ... For an thorough treatment of the subject of arrays and SQL Server, ... it looks like XML is probably our best bet in this situation. ... I guess it's not XML as such that's the problem, just the query optimizer ...
    (microsoft.public.sqlserver.xml)
  • Re: Event/Timing problem when loading data from xml...
    ... a series of function process that data ... > in the arrays for display on the web page. ... It is like to buy white shoes first and then polish them with black wax ... You don't need XML intermediary media. ...
    (comp.lang.javascript)