Hi Jason,

I have to apologize for the solution, it is not correct because it reduces an array with hashes with one.

Cheers,

Thiel

Op 18-5-2011 23:23, Thiel Chang schreef:
Hi Jason,

As I understand you well does it mean that the array in { :result => [ ... ] } may not contain hash elements, e.g. [ { :a => "x"}, ..., { :b => "z"} ] ?

When I have to use the subtree approach the rule/solution is something like:

rule( :result => subtree( :z ) ) {

z[0].each {|k,v|

@key = k

@val = v }

z[0]= Hash[@key, "newtmp"]

@ops= []

index= 0

z[1].each {|hsh|

hsh.each {|k|@ops[index] = k

index += 1}

}

z.delete_at(1)

{:result => z}

}

Just plain Ruby code.







Op 18-5-2011 22:46, Jason Garber schreef:
Oh, I see. In your example, there were hashes in the array, which wouldn't match a sequence. That would be a subtree. I've see now.

On Wed, May 18, 2011 at 2:44 PM, Thiel Chang <[email protected] <mailto:[email protected]>> wrote:

    Hi Jason,

    Many thanks for your response and I will articulate my problem again.

    My problem is that I don't understand how to apply the
    sequence(symbol) in a transformation rule. Althought,  I saw it
    in the Parslet examples it is still a fuzzy item for me.
    Therefore, I created  a problem and in the solution I want to use
    the sequence(symbol), if possible.

    Let the expression be: { :result => [{ :w => "a" }, { :x => "b" }
    , { :y => "c", :z => "d"} ] } .   So :result => [ ... ]  is a
    hash with :result as a key and the array [ ... ] as a value.

    I want this expression be transformed into: { :result => [ "imf",
    "strauss", "kahn" ] } :-)

    In the Parslet Transformation documentation it is said: ".....,
    but you cannot replace the array as a whole. This is the purpose
    of sequence(symbol):  sequence(:x) which will match all of these
[ 'a', 'b', 'c' ] [ 'a', 'a', 'a' ] [ Foo.new, Bar.new ] ... " Besides, sequence is very picky about what it
    consumes and what not. But that's not defined in CLEAR  way in
    this documentation and not ILLUSTRATED with examples. This part
    of the documentation is not very didactic.

    When I consider this problem as plain Ruby code it is easy to
    transform the code, but that's not the solution I want. My goal
    to understand the use of sequence in a transformation rule.

    Cheers,

    Thiel

    Op 18-5-2011 21:01, Jason Garber schreef:
    Thiel,
    :elements gets captured and becomes available in the block's
    context. If you want the output to be a hash, you have to have
    {:result => element } in the block. But then your transformation
    rule does nothing, so I don't see why you'd want to.

    I couldn't follow your example very well. Perhaps that's why no
    one has responded yet. Try articulating your problem again if
    you're still having trouble.

    Jason

    On Wed, May 18, 2011 at 5:55 AM, Thiel Chang <[email protected]
    <mailto:[email protected]>> wrote:

        Hi guys,

        I am still struggling with the transformation rules.:-(

My aim is to understand the application of sequence(symbol) in transformation rules.

         So, I made a simple test to replace an array of objects in
        a transformation rule.

        The input tree for Transform is:

        { :result => [{ :w => "a" }, { :x => "b" } , { :y => "c", :z
        => "d"} ] }

        and the transformation rule is:

        rule( :result => sequence(:elements) ) { puts
        elements.inspect  }

        The new array is: [ "imf", strauss", "kahn"]

        The required result should be:   { :result => [ "imf",
        "strauss", "kahn" ] }

        I would be very glad if anyone can provide the solution(s).

        Many thanks in advance,

        Thiel








Reply via email to