[MT 1.4.0.2 BUG - revisited] json.difference

Developer discussion regarding MapTool 1.4

Moderators: dorpond, trevor, Azhrei

Forum rules
Posting now open to all registered forum users.
Post Reply
User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

[MT 1.4.0.2 BUG - revisited] json.difference

Post by wolph42 »

a looong time ago this was discussed before: http://forums.rptools.net/viewtopic.php ... ce#p248437

and while working on the dungeon generator I hit this bug yet again. the workaround I created at the time alas does not work in this case and another workaround would make the current process so slow it would not be feasible.

In short: this time I *really* would like this bug fixed.

In short:

Code: Select all

[json.difference('[1,1,2,3,]','[1,3]')]
returns

Code: Select all

[2]
while it should return:

Code: Select all

[1,2]
there are functions like Wiki: json.removeAll() and Wiki: json.unique() that can be used to get what currently is the result of the bugged version of json.difference. So there is absolutely no reason for json.difference to work as it works.

Should be people start crying about backwards compatibility (which I doubt would be an issue) I'm happy if another function is added that does what it should do e.g. json.actualDifference or json.realDifference.
for the savvy observer
One might have spotted this thread. I have not started working on a solution for that, so this issue has nothing to do with that issue, although when actually starting working on the solution of that issue, this issue might become an issue for that issue as well, who knows 8)

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: [MT 1.4.0.2 BUG - revisited] json.difference

Post by aliasmask »

I agree. json.difference and json.removeAll are essentially the same function. This is how you would need to do it manually.

Code: Select all

[H: a = json.append("",1,1,2,3)]
[H: b = json.append("",1,2,4)]
[H: c = a]
[H, foreach(value,b), code: {
   [H: index = json.indexOf(c,value)]
   [H, if(index+1): c = json.remove(c,index)]
}]

[R: c] 
I added the 4 to show that the difference is just a - b rather than a xor b. If you wanted the xor of the 2 arrays you could json.append the value not found in the if statement.

edit: actually for the xor, just doing the json.append wouldn't work because if you have 2 or more of the same number in the b array, then it would remove them as it traversed the b array. You would need to put the values in b but not in a in to separate array and merge after the loop.

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: [MT 1.4.0.2 BUG - revisited] json.difference

Post by Craig »

Its not a bug :)

Its one of the set functions MT supports for JS objects so lives by the rules of set theory...
Having said that I will add a json.removeFirst(<array1>, <array2>) which will do what you want.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: [MT 1.4.0.2 BUG - revisited] json.difference

Post by wolph42 »

Craig wrote:Its not a bug :)

Its one of the set functions MT supports for JS objects so lives by the rules of set theory...
Having said that I will add a json.removeFirst(<array1>, <array2>) which will do what you want.
Thnx.

Post Reply

Return to “MapTool 1.4”