|
uniqIdObj
... if, however, you're looking for an easy way to obtain an unique symbol
whenever you need it, here is an object which supplies unique ids.
It basically has a counter which is incremented with every access and
then outputs the value with a prefix ("_") as a base 36 number.
This guaranties compact symbols even for large numbers :
dobj = new (script "UniqIdObj")
dObj.setLastId (the maxInteger - 1)
put dObj.getUniqId()
-- "_ZIK0ZJ"
-- parent script UniqIdObj
property myLastID,myLDigits,myPraefix
on new me
init me
return me
end
on init me
myPraefix = "_"
myLDigits = getLDigits(me)
setLastId me
end
on setLastId me,dId
-- to initialize the counter with some random integer
if voidP(dId) then
dId = "0"
else
dId = string(dId)
end if
myLastID = integer(dId)
end
--------------
on getLDigits me
-- returns a list of 36 digits 0..9,A..Z
myLDigits = []
repeat with i = 0 to 9
myLDigits.append( string(i) )
end repeat
repeat with i = 0 to 25
myLDigits.append( numToChar(65 + i) )
end repeat
return myLDigits
end
on toBase me,dVal,dBase,sID
if voidP(sID) then sId = ""
if dBase > myLDigits.count then return #illegal_Base
if not integerP(dBase) then dBase = myLDigits.count
if not integerP (dVal) then return #bad_Input
high = dVal / dBase
low = dVal mod dBase
sID = string( myLDigits[low + 1] ) & sID
if high > 0 then
return toBase (me,high,dBase,sId)
else
return sID
end if
end
on getUniqid me
myLastId = myLastId + 1
return myPraefix & tobase (me,myLastId)
end
|