Table of Contents

About

Decodes a string of key-value pairs data and returns a flat list of keys and values.

Example Usage

When the function is called with:

wasKeyValueDecode("position=<1,1,0>&toggle=on");

it will return the list of strings:

[ "position", "<1,1,0>", "toggle", "on" ];

Consider using string to vector for converting the <1,1,0> string to a vector.

Code

LSL

This script was tested and works on OpenSim version 0.7.4!

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
list wasKeyValueDecode(string data) {
    return llParseString2List(data, ["&", "="], []);
}

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Decodes key-value pair data to a dictionary.
/// </summary>
/// <param name="data">the key-value pair data</param>
/// <returns>a dictionary containing the keys and values</returns>
private static Dictionary<string, string> wasKeyValueDecode(string data)
{
    Dictionary<string, string> output = new Dictionary<string, string>();
    foreach (string tuples in data.Split('&'))
    {
        string[] tuple = tuples.Split('=');
        if (!tuple.Length.Equals(2))
        {
            continue;
        }
        if (output.ContainsKey(tuple[0]))
        {
            continue;
        }
        output.Add(tuple[0], tuple[1]);
    }
    return output;
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2014 - License: CC BY 2.0      ##
###########################################################################
function wasKeyValueDecode($data) {
    preg_match_all("/(.+?)=(.+?)(&|$)/", $data, $match);
    return array_combine($match[1], $match[2]);
}

fuss/data_structures/key-value_pairs/decode.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.