How do you deal with Strings that can also be numbers?

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
User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

How do you deal with Strings that can also be numbers?

Post by Jagged »

I have run into a couple problems recently with string manipulation. I am storing reasonably large amounts of data in strings where each character can be a value between "0" and "9" and "A" to "F". Dealing with individual characters has been okay, but occasionally I need to handle longer strings and occasionally the fact that those longer strings start with "0" is really really important. This is when the script language's type-less state becomes a real pain. For example:

Code: Select all

[h:s="0"]
S=[r:s]<br>
IndexOf=[r:indexOf("01234",s)]<br>
[h:fData="01234"]
IndexOf=[r:indexOf(fData,s)]<br>
fData=[r:fData]<br>
What results would you expect from this? Unfortunately you get 0 and -1 because fData gets changed from "01234" to 1234 :(

Now when this occurs in the middle of a nest of recursive functions and when the strings rarely start with a "0", this is a royal pain in the stack to debug. I don't really want to stick a "Z" on the end of every string (which solves the problem above). So I was wondering how other people deal with this issue?

Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Re: How do you deal with Strings that can also be numbers?

Post by Ditto »

Jagged wrote: I don't really want to stick a "Z" on the end of every string (which solves the problem above).
This is typically the trick, though. Take a look at Hex representations:

http://en.wikipedia.org/wiki/Hexadecimal

Some use:
#36
some use:
0h36
.. etc.

but in all cases, they end up tacking "something" on. You will probably find that the simplest solution ..

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

Re: How do you deal with Strings that can also be numbers?

Post by wolph42 »

yup, its a known issue. Its even worse as this also happens with token ids that don't contain an alpha character. The only way round is indeed start with an alpha character.

Post Reply

Return to “Macros”