Table of Contents

About

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

Comments

  • Requires a list of variables and values, eg: [ "variable_1", "value_1", "variable_2", "value_2" ].
  • Returns the encoded string, eg: variable_1=value_1&variable_2=value_2.

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);
}

fuss/data_structures/key-value_pairs/encode.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


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