About

Merges the key-value pair string kvp with the parameters from a string data and returns the result as a key-value pair.

Comments

  • Requires data and kvp string in description format, eg variable_1=value_1&variable_2=value_2.
  • Updates the key-value pair kvp, changing or adding the new variables from data.
  • Returns a key-value pair string.

Example Usage

Updating Variables

Suppose that the object description is:

	 
toggle=on&target=<0,0,1>&sound=off	 

and that we call the function as:

llSetObjectDesc(wasKeyValueMerge("toggle=off&sound=on", llGetObjectDesc()));	 

then the object description will be updated to:

	 
toggle=off&target=<0,0,1>&sound=on	 

Merging Updates

Suppose that the object description is:

	 
toggle=on&target=<0,0,1>&sound=off	 

and that we call the function as:

llSetObjectDesc(wasKeyValueMerge("toggle=off&special=k", llGetObjectDesc()));	 

then the object description will be updated to:

	 
toggle=off&target=<0,0,1>&sound=off&special=k	 

The extra key-value pair special=k gets added.

Algorithm

  1. explode the two key-value data segments on = and & symbols.
  2. pass the two exploded lists to left, respectively right of wasKeyValueMergeTuple.
  3. check if both lists are empty.
    1. if they are, return the empty list.
  4. if the left list is empty, swap left and right and empty right ($left = 0$, $left := right$, $right := 0$).
  5. pop two elements x and z off the left list
    1. if the first element x can be found in right, assign the value of right to z.
  6. return tuple x=z + recurse at 3.
  7. dump all tuples x=z by &.

Example

[ a b c d e f ] | [ x z a e r t ]
[ c d e f | x z r t ] | a=e
[ e f | x z r t ] | a=e c=d 
[ | x z r t ] | a=e c=d e=f 
// --- left = 0, left := right, right := 0
[ x z r t | ] | a=e c=d e=f
[ r t | ] | a=e c=d e=f x=z 
[ | ] | a=e c=d e=f x=z r=t

Properties

  • The algorithm has $O(\frac{left}{2})$ complexity. This is because the left list consists of tuples of $(i,i+1)$ and only for all keys $i$, the algorithm will try to find a match in the right list.

Code

This script was tested and works on OpenSim version 0.7.4!

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
list wasKeyValueMergeTuple(list left, list right) {
    if(llGetListLength(left) == 0 && llGetListLength(right) == 0) 
        return [];
    if(llGetListLength(left) == 0) {
        left = right;
        right = [];
    }
    string lk = llList2String(left, 0);
    left = llDeleteSubList(left, 0, 0);
    string lv = llList2String(left, 0);
    left = llDeleteSubList(left, 0, 0);
    integer x = llListFindList(right, (list)lk);
    if(x != -1) {
        lv = llList2String(right, x+1);
        right = llDeleteSubList(right, x, x+1);
    }
    return [lk + "=" + lv] + wasKeyValueMergeTuple(left, right);
}
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueMerge(string kva, string kvb) {
    return llDumpList2String(
        wasKeyValueMergeTuple(
            llParseString2List(kva, ["=", "&"], []),
            llParseString2List(kvb, ["=", "&"], [])
        )
    ,"&"
    );
}

fuss/data_structures/key-value_pairs/merge.txt ยท Last modified: 2019/01/03 01:38 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.