[b87,89,90] BUG: FOREACH strips some leading zeros

Confirmed bugs should get a single post here. Check the READ ME FIRST sticky thread for the format.

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice, MapTool BugReport Manager

Forum rules
Posts that do not conform to the READ ME FIRST sticky thread are subject to deletion.
User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

[b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

For some strings, Wiki: FOREACH ()will trim leading zeros from a number string...

Code: Select all

[ t1 = json.append("[]","00000000547494000975000000000000","000000005C9994003576000000000000","00000000389894002776000000000000","000000001B699400AE74000000000000")]<br>
[FOREACH(i,t1),CODE: {
  [ i ]<br>
}] 
Output is:

Code: Select all

["00000000547494000975000000000000","000000005C9994003576000000000000","00000000389894002776000000000000","000000001B699400AE74000000000000"]
 547494000975000000000000 ,
 000000005C9994003576000000000000 ,
 389894002776000000000000 ,
 000000001B699400AE74000000000000
Last edited by lmarkus001 on Sat Aug 23, 2014 2:47 pm, edited 1 time in total.

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

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by aliasmask »

If it looks like a number, then MT will convert a string to a number when assigning it to a variable or when outputting to chat. If it's not supposed to represent a number, I would add a character at the beginning of the string.

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

You are missing the bug. ONLY some of those get converted to numbers and have the leading zeros stripped.

And this issue was found from dealing with token ids. What you see are 4 token ids returned in a json array from a function. When I try to loop through that array of ids, a very small percentage of them get leading zero stripped.

So if I were to try to loop through and get the name of those tokens with Wiki: getName(), I would get an error from some un-guessable list of tokens whos ids get leading zero stripped!

So ANY function I pass this ID too will fail as it sees a non-32 character string.

I uncovered this in my game session last night when I could not bring up the attack dialog. After poking around, it turns out 2 of the tokens on the map have IDs that get leading zero stripped. This is the first time I have encountered this behavior but it has always been there. Bizarre.

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

Oh and so I tried adding "ID" to the lead of that string (so it would now be a 34 character string starting with obvious text characters), and then stripping the "ID" from it with Wiki: replace ()or Wiki: substring(), but once the "ID" is removed from the front of the string, it now gets leading zero stripped.

So any token with these "magical" IDs becomes fairly non-macro friendly as their ID cannot be used by functions that use an ID as a parameter!!!

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

Trying to make it a string...

Code: Select all

[H: t1 = json.append("[]","00000000547494000975000000000000","000000005C9994003576000000000000","00000000389894002776000000000000","000000001B699400AE74000000000000")]<br>
[FOREACH(i,t1),CODE: {
[ j = i ]
[k = j ]
[tiL = length(i)]<br>
[IF(tiL != 32 ): j = trim(" " +  substring("00000000000000000000000000000000",0,32-tiL)+j ); "ok" ]<br>
[IF(tiL != 32 ): k = substring(("ID" +  substring("00000000000000000000000000000000",0,32-tiL)+k),2,34); "ok" ]<br>
  [ i ]<br>
  [ j ]<br>
  [k]<br><br>
}] 
Renders this output (note that no matter what the ID becomes useless):

Code: Select all

547494000975000000000000 547494000975000000000000 24
 00000000547494000975000000000000
 00000000547494000975000000000000
 547494000975000000000000
 547494000975000000000000
 547494000975000000000000

  , 000000005C9994003576000000000000 000000005C9994003576000000000000 32
 ok
 ok
 000000005C9994003576000000000000
 000000005C9994003576000000000000
 000000005C9994003576000000000000

  , 389894002776000000000000 389894002776000000000000 24
 00000000389894002776000000000000
 00000000389894002776000000000000
 389894002776000000000000
 389894002776000000000000
 389894002776000000000000

  , 000000001B699400AE74000000000000 000000001B699400AE74000000000000 32
 ok
 ok
 000000001B699400AE74000000000000
 000000001B699400AE74000000000000
 000000001B699400AE74000000000000
Last edited by lmarkus001 on Sat Aug 23, 2014 2:47 pm, edited 1 time in total.

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

At the moment, the only solution I have is to test any ID, and error out if it is one of the rare invalid ones...

Code: Select all

[H: tToken = currentToken() ]
[H: assert( length(tToken) == 32, "Invalid Token ID ("+tToken+"): Recommend cut-paste to create new ID")] 

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

UGLY solution:

Any time you call a function and pass it an ID that was being held by a temporary variable, encapsulate the ID in a Wiki: strformat () inside the function call itself.

Here in an overly simplified example, I demonstrate:

Code: Select all

[ id = currentToken() ]
[ getName( strformat("%032d",id) ) ] 

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

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by aliasmask »

Yeah, Wolph42 discovered that bug where the id hash looks like a number. You know what, I think I just discovered why my target id works most of the time and sometime doesn't... I'll have to do the same fix apparently. Yep, this is annoying.

The ID is stored correctly but the act of getting it changes the value. I'll have to play around with a few methods to see if I can trick MT in to doing it right.

edit: Yeah, that was the first thing I was thinking about, but I wasn't sure how it would act if it didn't see it as a number. So, there would be another step to that as well. I'm thinking switchToken may see an id that changed to a number and may actually switch to correct token. Then you can just use currentToken for the id string.

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

This is a "bad ID" detector macro. It will report any tokens with an ID that will have its leading zeros stripped...
So far just the two from my game session last night have this issue...

Code: Select all

[H: selectTokens()]
[H: ids = getSelected()]
[H: badids = "[]"]
[H,FOREACH(id,ids),CODE: {
  [ tlen = length(id) ]
  [IF( tlen != 32 ): badids = json.append(badids,id); ""]
}]
[R,FOREACH(id,badids,""): "<b>Bad ID:</b> " + getName(strformat("%032d",id)) + " ("+id+")<br>" ]
[R: json.length(badids) + " IDs are bad."] 

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

I am not going to rework my code for this issue. Instead I will just use the Bad ID Detector macro and cut-paste any tokens that have an issue.

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

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by aliasmask »

You can probably just use Wiki: isNumber() to detect a bad id.

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

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by aliasmask »

If you run across a bad id, can you try to source the token? Was it a copied token? Is it in your resource library? Did you Drag'n'drop it from explorer? Did a player drop the token? Did the token come from IMarv's collection or Hero (something)?

User avatar
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by lmarkus001 »

I had pasted it. I had a token I copied, then pasted 30 copies of it, 2 of those 30 had the trouble IDs.

Looking at the IDs, the trouble ones have no letters in them so they indeed look like numbers.

The two solutions I have are:
1) run the detection macro and remove trouble tokens
or
2) Change every single id passing/using line of code to put the strformat(%032d",id) in place of id. This is really icky.


User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: [b87,89,90] BUG: FOREACH strips some leading zeros

Post by Full Bleed »

lmarkus001 wrote:Looking at the IDs, the trouble ones have no letters in them so they indeed look like numbers.
So the "fix" would be for MT to make sure every ID is created with at least one character in it?

Probably something to pass along to the mote guys to get this fix in.
Last edited by Full Bleed on Mon Aug 25, 2014 9:07 am, edited 1 time in total.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

Post Reply

Return to “Bug Reports”