Table of Contents

About

Encodes a list of variables and values to the key-value pair format.

Comments

Example Usage

When called with:

wasKeyValueEncode(["sound", "off", "time", "1365161334"]);

the function will return the string:

sound=off&time=1365161334

Code

LSL

This script was tested and works on OpenSim version 0.7.4!

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueEncode(list data) {
    integer i = llGetListLength(data);
    if (i % 2 != 0 || i == 0) return "";
    --i;
    do {
        data = llListInsertList(
            llDeleteSubList(
                data, 
                i-1, 
                i
            ),
            [ llList2String(data, i-1) + "=" + llList2String(data, i) ], 
            i-1
        );
        i -= 2;
    } while(i > 0);
    return llDumpList2String(data, "&");
}

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Serialises a dictionary to key-value data.
/// </summary>
/// <param name="data">a dictionary</param>
/// <returns>a key-value data encoded string</returns>
private static string wasKeyValueEncode(Dictionary<string, string> data)
{
    List<string> output = new List<string>();
    foreach (KeyValuePair<string, string> tuple in data)
    {
        output.Add(string.Join("=", new [] {tuple.Key, tuple.Value}));
    }
    return string.Join("&", output.ToArray());
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2014 - License: CC BY 2.0      ##
###########################################################################
function wasKeyValueEncode($data) {
    array_walk($data,
        function(&$value, $key) {
            $value = $key."=".$value;
        }
    );
    return implode('&', $data);
}