About

Returns the value of the key k from the key-value pair data, or if the variable k is not set or empty the function returns the empty string.

Example Usage

Suppose that the object's description is set to:

data=empty&movement=off

when calling the function with:

wasKeyValueGet("movement", llGetObjectDesc());

the function will return the string "off".

Now suppose that the object's description is set to:

data=empty&movement=off

and we call the function with:

wasKeyValueGet("time", llGetObjectDesc());

the function will return the empty string ("").

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 wasKeyValueGet(string k, string data) {
    if(llStringLength(data) == 0) return "";
    if(llStringLength(k) == 0) return "";
    list a = llParseString2List(data, ["&", "="], []);
    integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
    if(i != -1) return llList2String(a, 2*i+1);
    return "";
}

C

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
char *wasKeyValueGet(char *key, char *data) {
    if (key != NULL && data != NULL && strcmp(key, "") != 0 && strcmp(data, "") != 0) {
        char *pair, *value;
        data = strdup(data);
        while ((pair = strsep(&data, "&")) != NULL)
            if (strcmp(strtok(pair, "="), key) == 0 && (value = strtok(NULL, "&")) != NULL)
                return value;
    }
    return "";
}

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Returns the value of a key from a key-value data string.
/// </summary>
/// <param name="key">the key of the value</param>
/// <param name="data">the key-value data segment</param>
/// <returns>true if the key was found in data</returns>
private static string wasKeyValueGet(string key, string data)
{
    foreach (string tuples in data.Split('&'))
    {
        string[] tuple = tuples.Split('=');
        if (!tuple.Length.Equals(2))
        {
            continue;
        }
        if (tuple[0].Equals(key, StringComparison.Ordinal))
        {
            return tuple[1];
        }
    }
    return string.Empty;
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: CC BY 2.0      ##
###########################################################################
function wasKeyValueGet($key, $data) {
    return array_reduce(
        explode(
            "&", 
            $data
        ),
        function($o, $p) use($key) {
            return array_reduce(
                    explode(
                        "=",
                        $p
                    ),
                    function($q, $r) use($key) {
                        if($q === $key) return $r;
                    },
                    $key
            ). $o;
        }
    );
}

Python

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0      ##
###########################################################################
def wasKeyValueGet(key, data):
    try:
        return dict(
            pair.split("=")
                for pair in data.split("&")
            ).get(key, None)
    except:
        return None

JavaScript

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2019 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
function wasKeyValueGet(k, data) {
    if(data.length == 0) return "";
    if(k.length == 0) return "";
    var a = data.split(/&|=/);
    var i = a.filter((e,i) => !(i % 2)).indexOf(k);
    if(i != -1) return a[(2 * i) % a.length + 1];
    return "";
}

fuss/data_structures/key-value_pairs/get.txt ยท Last modified: 2019/04/27 16:57 by office

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.