in reply to Re: the annoying keys "key" and "value"
in thread the annoying keys "key" and "value"

I presumed a level of experience that is much less common than I expected, hence the seeming cryptic nature of my message. Sorry.

This pathological representation of a hash is in wide-spread use due to some idiotic standards adopted early in the history of XML. It shows up in Perl frequently when dealing with XML. In the most recent situation I've encountered, WordPress XML-RPC returns its custom fields in such a representation. There are even some WordPress XML-RPC libraries in some languages that require you to pass in an ordinary hash rather than this pathological (array of hashes) representation of a hash -- thereby hiding the pathology.

I was hoping to discover the generic name for this pathological hash representation to search for packages that specifically deal with it so as to hide the pathology.

  • Comment on Re^2: the annoying keys "key" and "value"

Replies are listed 'Best First'.
Re^3: the annoying keys "key" and "value"
by LanX (Saint) on Dec 23, 2010 at 22:10 UTC
    IIRC XML allows multiple children-elements of same tag-name (like - as already mentioned - some HTML-Form elements do) and they are ordered.

    Ordinary hashes in perl don't allow multiple keys of same name and have no order.

    I doubt there is any common name, you might call it "XML-structure".

    Cheers Rolf

      Thanks for the cogent response -- although I expect someone has come up with a term for this kind of annoying representation of a hash.

      PS: I understand the "reason" for the pathological representation. The non-pathological representation would have:  $hash{$key}=[$value1, $value2, $value3,...] and the distinction between array and scalar fields would be handled by the schema.

        I'm not sure that "pathological" describes this structure because it is one of the good ways in Perl to represent some types of C 2-D structures.

        It appears, as it turns out, a less general and more specific Perl structure, a HashOfArray would have been better.

        Sorry that I have no specific name in your application for this structure or knowledge of a standard module to convert between various structure types. But as seen in this thread, such a conversion is "not hard".

        > The non-pathological representation would have: $hash{$key}=[$value1, $value2, $value3,...] and the distinction between array and scalar fields would be handled by the schema.

        I don't understand what you want or mean, sorry.

        Could you give me an example of a non trivial XML with a reversable mapping to a nested perl data structure?

        For my amateur understanding each XML-tag is from a perl perspective a hash with attributes as keys additionally to tag name and an ordered list of children tags. And the list part can only be represented as an array or a scalar if it's just one element (like a text-string).

        The example you gave in the OP is only a special case of a simple schema, where "key" was the tag-name and "value" was a (one element) string.

        For sure many DTDs might be much easier - or in your word "non-pathologically" - represented, but thats the general case we are talking about.

        Cheers Rolf

Re^3: the annoying keys "key" and "value" (XML-RPC)
by LanX (Saint) on Dec 24, 2010 at 09:59 UTC
    > In the most recent situation I've encountered, WordPress XML-RPC returns its custom fields in such a representation.

    after looking up XML-RPC#Data_types on wikipedia I encountered the following "workaround" to represent associative arrays in XML:

    <struct> <member> <name>foo</name> <value><i4>1</i4></value> </member> <member> <name>bar</name> <value><i4>2</i4></value> </member> </struct>

    Is this part of the scheme your problem?

    In my eyes it's XML which is pathological here, and CPAN modules just try to reflect the structure to avoid ugly bugs caused by information loss when cross transforming data...

    For instance:

    • Can you guaranty that no implementation XML-RPC-parser will ever try to pull information out of the order of the "members"?

    • Do they handle repeated keys alike?

    And if you think it's so well defined, so why don't you just hack your own map_to_pathological(%hash) routine which simplifies your task?

    CPAN authors might show interest in your patch, or you publish your own add-on module.

    Cheers Rolf

Re^3: the annoying keys "key" and "value"
by Anonymous Monk on Dec 23, 2010 at 23:34 UTC
    I don't understand , how it is pathological (having properties that cause unusually bad behaviour, especially regarding correctness or performance)?
      I believe he means "unduly hard to use". That may or may not be true depending on what's needed of the data.
        Hmm, that doesn't clarify it for me