Table of Contents

About

Deletes a key k and its corresponding value from a key-value pair.

Examples

Suppose the object's description is set to:

sound=on&time=now&position=<1,0,0>

When the function is called with:

llSetObjectDesc(wasKeyValueDelete("position", llGetObjectDesc()));

the object's description will be updated to:

sound=on&time=now

Code

LSL

This script was tested and works on OpenSim version 0.7.4!

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueDelete(string k, string data) {
    if(llStringLength(data) == 0) return "";
    if(llStringLength(k) == 0) return "";
    integer i = llListFindList(
        llList2ListStrided(
            llParseString2List(data, ["&", "="], []), 
            0, -1, 2
        ), 
    [ k ]);
    if(i != -1) return llDumpList2String(
        llDeleteSubList(
            llParseString2List(data, ["&"], []),
        i, i), 
    "&");
    return data;
}

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Deletes a key-value pair from a string referenced by a key.
/// </summary>
/// <param name="key">the key to search for</param>
/// <param name="data">the key-value data segment</param>
/// <returns>a key-value pair string</returns>
private static string wasKeyValueDelete(string key, string data)
{
    List<string> output = new List<string>();
    foreach (string tuples in data.Split('&'))
    {
        string[] tuple = tuples.Split('=');
        if (!tuple.Length.Equals(2))
        {
            continue;
        }
        if (tuple[0].Equals(key, StringComparison.Ordinal))
        {
            continue;
        }
        output.Add(string.Join("=", tuple));
    }
    return string.Join("&", output.ToArray());
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: CC BY 2.0      ##
###########################################################################
function wasKeyValueDelete($key, $data) {
    $data = explode("&", $data);
    $data = array_filter(
        array_combine(
            array_map(
                function($o) {
                    return array_shift(explode("=", $o));
                }, 
                $data
            ), 
            array_map(
                function($o) {
                    return array_pop(explode("=", $o));
                }, 
                $data
            )
        )
    );
    if (array_key_exists($key, $data)) {
        unset($data[$key]);
    }
    array_walk($data,
        function(&$value, $key) {
            $value = $key."=".$value;
        }
    );
    return implode('&', $data);
}

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