ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I am giving a small snippet of my code. I am pushing elements into the hash in this way.

$main_hash{"query"}{"filtered"}{"query"}{"bool"}{"must"} = \@file_arra +y; $main_hash{"query"}{"filtered"}{"filter"} = $condition_array[(int($con +dition)-1)];

I need the query in the first place. But getting the filter in the first place. How to bind the query in first place???? Expected output (After changing into JSON):

{ "query": { "filtered": { "query": { "bool": { "must": [ {"wildcard": { "path": "/home/GEMS/CDRS/Delivery/ravi_16051820*.log" } } ] } }, "filter": { "bool": { "should": [ { "term": { "OrigInterface": "SMPP" } }, { "term": { "OrigInterface": "SMPP" } } ] } } } } }

Actual output

{ "query": { "filtered": { "filter": { "bool": { "should": [ { "term": { "OrigInterface": "SMPP" } }, { "term": { "OrigInterface": "SMPP" } } ] } }, "query": { "bool": { "must": [ {"wildcard": { "path": "/home/GEMS/CDRS/Delivery/ravi_16051820*.log" } } ] } } } } }

Replies are listed 'Best First'.
Re: Maintainance of element order in hash
by hippo (Chancellor) on Sep 29, 2016 at 12:50 UTC

      In seen  Tie::IxHash but in this I am seeing only key pair value. But I dont understand how to give nested hashes into it. Can you write a small snipplet for the above example using Tie::Ixhash

        I dont understand how to give nested hashes into it
        #!/usr/bin/env perl # Hash order maintained use strict; use warnings; use Test::More tests => 1; use Tie::IxHash; tie my %foo, 'Tie::IxHash'; $foo{a} = [3, 2, 1]; $foo{b} = { s => 'senatus', p => 'populus', q => 'que', r => 'romanus' + }; is_deeply ([keys %foo], [qw/a b/], 'Order retained');

        and see perldsc for how to use nested data structures generally.

Re: Maintainance of element order in hash
by jellisii2 (Hermit) on Sep 29, 2016 at 13:02 UTC
Re: Maintainance of element order in hash
by davido (Cardinal) on Sep 30, 2016 at 05:12 UTC

    Are you absolutely committed to relying on JSON objects preserving order? If your design mandates that to be the case, you would be better off making your filtered element contain an array of objects rather than object elements. This is a problem that my be solved by one level of indirection.

    If there is any possibility that you can annul your marriage to a design that requires ordered JSON objects, I think you should give that possibility due consideration.

    Here are a few reasons why:

    • Wikipedia: Object: an unordered collection of name/value pairs...
    • JSON.org states, "An object is an unordered set of name/value pairs."
    • The ECMA standard is silent on defining object ordering, but is explicit in asserting that arrays are ordered.
    • RFC4627 does not define how objects are to be ordered, and therefore makes no guarantees. It does, however, mandate that a JSON parser MUST accept all texts that conform to the JSON grammar. It stands to reason, then, that all conforming JSON parsers do not need to be sensitive to object ordering.
    • RFC7159 again omits any guarantees about object order, and goes so far as to include this advice:
      "JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.

    Perl's hashes have a high degree of parity with JSON objects. Standard Perl hashes do not maintain any concept of static order. You can tie a Perl hash to a class that maintains order, but in doing so you make tradeoffs, giving away speed and memory for reliable ordering. Is it worth the tradeoff? If so, you may need to also rethink the serialization format you are using, because JSON isn't guaranteed to serialize and deserialize objects in any particular order. Any guarantees would have to come from the implementation. As an example, JSON parsers such as JSON::XS allow you to specify canonical ordering for object keys. But canonical is used to mean a sorted order. Not a user-specified order.