json.remove multiple times on the same array used in a loop

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Shadow Wizard
Giant
Posts: 192
Joined: Mon Apr 11, 2011 8:11 pm

json.remove multiple times on the same array used in a loop

Post by Shadow Wizard »

So, I have a json array, and I need to cycle through it to find each instance something happens, pop up a notification, and then remove it from the json array.
The problem is, if the "something happens" triggers more than once my roll.count no longer correctly corresponds to the location in the json array, so ends up resulting in an error. Her is the offending snippit of code..

Code: Select all

[h:newrounds=rounds]
[h:actiontoreport=""]
[h, FOREACH(curjson, rounds), CODE: {
	[h:curjsonround=json.get(curjson, "Round")]
	[h, if(curjsonround<moveday), CODE: {
		[h:actiontoreport=actiontoreport+json.get(curjson, "Action")+", "]
		[h:newrounds=json.remove(newrounds, roll.count)]
		[h:input(
			"nothing| The following action has occured: | |LABEL|SPAN=TRUE",
			"nothing|"+json.get(curjson, "Action")+"| |LABEL|SPAN=TRUE"
			)]
	};{}]
	
}]
I am at a loss on how to do this.

Shadow Wizard
Giant
Posts: 192
Joined: Mon Apr 11, 2011 8:11 pm

Re: json.remove multiple times on the same array used in a loop

Post by Shadow Wizard »

After posting this, I just figured it out.. However am curious how someone else would suggest I do it..

I will also, of course mention my solution for anyone that may find this in the future.
Basically, I added a counter that only increases when the if function fails. Then if the if function succeeds, instead of using roll.count as an index to remove the section required I used the counter.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: json.remove multiple times on the same array used in a loop

Post by aliasmask »

Code: Select all

[h: newrounds = "[]"]
[h: actiontoreport = "[]"]
[h, FOREACH(curjson, rounds), CODE: {
   [h, if(json.get(curjson, "Round") < moveday): actiontoreport = json.append(actiontoreport,json.get(curjson, "Action"));
      newrounds = json.append(newrounds,curjson)]
}]
[H: rounds = newrounds]
[H, if(! json.isEmpty(actionreport)): broadcast("Actions Taken:<br>"+json.toList(actionreport),"<br>")]
I just post to chat, but you could put in a dialog as well. I didn't see a need to pause execution for each action since no interaction was needed. I put the actions used in the report and the ones not used in to new array of objects then overwrite original.

As a rule of thumb, never overwrite original during the process. You either make a copy or new object then overwrite at the end.

Shadow Wizard
Giant
Posts: 192
Joined: Mon Apr 11, 2011 8:11 pm

Re: json.remove multiple times on the same array used in a loop

Post by Shadow Wizard »

Ah, create a new list instead of modifying the existing one. I don't know why I didn't think of that as I used that exact same method on another macro I wrote. But thank you for taking the time. Helps me look at the programming I do from a different side.
You are awesome as always.

Post Reply

Return to “Macros”