substring() Indexing

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.
Post Reply
User avatar
Tanthos
Giant
Posts: 176
Joined: Thu Mar 28, 2013 6:55 pm
Location: United States

substring() Indexing

Post by Tanthos »

I noticed something today. The wiki entry for substring() states that it "Returns the substring of the string from the start to the end indexes."

This leads one to believe that the start and end parameters are both supposed to be treated as string indexes (which start at 0). However, only the start integer is treated as an index. The end parameter, however, starts at 1.

Example:

Code: Select all

[r:substring("0123456789", 0, 5)]
Returns: "01234"
Rather than: "012345" as it implies.

This is in MapTool 1.3.b90-beta.

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

Re: substring() Indexing

Post by aliasmask »

It's similar logic to the [for:] loop. The 2nd parameter is the non-inclusive end of loop. So, if you go from character 0 to 10, then you're only getting 10 characters (0-9) where 10 is the terminus and is not included because it's the start of the loop where the check is made.

User avatar
Tanthos
Giant
Posts: 176
Joined: Thu Mar 28, 2013 6:55 pm
Location: United States

Re: substring() Indexing

Post by Tanthos »

Ah. Then I heartily recommend altering the substring wiki page with a little note about that, because it is a little tricky as it is now.

Thank you for explaining that. I'll keep that logic in mind, and hopefully not get confused again.

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

Re: substring() Indexing

Post by wolph42 »

Tanthos wrote:Ah. Then I heartily recommend altering the substring wiki page with a little note about that, because it is a little tricky as it is now.

Thank you for explaining that. I'll keep that logic in mind, and hopefully not get confused again.
It's a wiki, feel free to update.

User avatar
Tanthos
Giant
Posts: 176
Joined: Thu Mar 28, 2013 6:55 pm
Location: United States

Re: substring() Indexing

Post by Tanthos »

Wow, I can do that? Sweet. How do I log in? It says my username is non-existent.


Post Reply

Return to “Bug Reports”