CraftTweaker

CraftTweaker

151M Downloads

How can I remove map entries by key?

Weisstrieb opened this issue ยท 1 comments

commented

Intro: Ambiguity between variables and string without double quotation marks

Issue Description:

In zenscript, the associative array(map) is declared by {<key> : <value>}. and if a string without double quotation marks is located in the position of <key>, it is automatically identified to its string value.

but if I want to remove map entries using keys that are reference variables in the script, How can I remove map entries?

Script used (Please Pastebin or gist your script, posting an unpasted or ungist'd script will automatically close this issue):

For example, let's assume some script lines in IRecipeFunction. I tried to remove map entries like below.

var newTag = ins.item.tag;

// Type of 'Inventory' is DataMap because it is TagCompound in the tag.
var invMap = newTag.Inventory.asMap();

for key in invMap {
    var invID = invMap[key].id.asString();
    if (some_map in invID) {
        var newInv = newTag.Inventory;
        var remove = {key : invMap[key]} as IData();
        newTag.Inventory.update(newInv - remove);
    }
}
return out.withTag(newTag);

I want to get a variable 'remove' as { <Real key value in the variable 'key'> : <value of invMap[key]> }.
However, the result is { "key" : <value of invMap[key]> }.

here is my original script: https://pastebin.com/SB6JT2m8


Affected Versions (Do not use "latest"):

  • Minecraft: 1.12.2
  • Forge: 14.23.2.2618
  • Crafttweaker: 4.0.12
commented

Ah, {key as IData : value} returns the result as I wanted, Close the issue.