Magic User Group

Magic Wiki-Wakka : Generate3LetterMagicVariablesFromNumber

HomePage :: CATEGORIES | Index :: Changes :: Comments :: Search :: Login/Register

How to Generate a 3-letter MagicVariable out of a Number


This routine can be used in a debugger to generate the Magic Variablenames or when you generate some exportfiles of a program yourself.

Task properties: Batch
EndTask: Yes/Before


The record main will look like this:
Select Parameter i-VariableNumber  "A"  Num 5      
Select Parameter o-VariableString  "B"  Alpha U3   

Select Virtual   v-NumPart1        "C"  Num 5               
Select Virtual   v-NumPart2        "D"  Num 5               
Select Virtual   v-NumPart3        "E"  Num 5               
Select Virtual   v-Part1           "F"  Alpha U             
Select Virtual   v-Part1           "G"  Alpha U             
Select Virtual   v-Part1           "H"  Alpha U             


The record suffix will look like this:
Update C   Expr: (A-1)/676
Update D   Expr: Fix ((A-Fix ((A-1)/676,5,0)*676-1)/26,5,0)
Update E   Expr: (A-1) MOD 26
Update F   Expr: CHR (C+65)     Cnd: A>676
Update G   Expr: CHR (D+65)     Cnd: A>26
Update H   Expr: CHR (E+65)     	

Update B   Expr: Trim (F)&Trim (G)&Trim (H)



I also received a nice theoritical look (and a program) on this routine from T.M. (thx)

Well, it seems to me that Magic's method of lettering variables is really
just another numbering system like binary, decimal, or hexadecimal. I
certainly don't know what the name would be for base 26, but this is pretty
much what's going on.

Some special considerations:

1. Magic starts counting at one, while numeric systems start counting at
zero. The formulas will have to be adjusted for that.

2. The first variable in the stack is really "AAA" (= to 000), but magic
doesn't show the leading "A" characters (like we don't usually show leading
0 digits).

Numeric systems are based upon exponential math.

Decimal:
10^0 = 1
10^1 = 10
10^2 = 100
10^3 = 1000

Binary:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
etc.

Magic: (base 26)
26^0 = 1
26^1 = 26
26^2 = 676

Considering that "A" in magic is equal to 0 in decimal....

If we wanted to go from magic variable name "CDB" to a number, we would:

Multiply the "B" (decimal equivalent of 1) * 1 = 1
Multiply the "D" (decimal equivalent of 3) * 26 = 78
Multiply the "C" (decimal equivalent of 2) * 676 = 1352
Sum the three products 1 + 78 + 1352 = 1431, and add 1 (because magic
starts at one, not zero), so the value CDB (magic) = 1432 (decimal)



Doing that backwards? From Right to Left (ones, 26's, 676's)

Start with = 1432
Subtract 1 = 1431
Mod 26 = 1 (the 1's column)
subtract this = 1420
mod 676 = 78
divide by 26 = 3 (the 26's column)
subtract 78 from 1420 = 1352
divide by 676 = 2 (the 676's column)


The record main will look like this:
select param   "A" Source Number        Num 5
select param   "B" Magic Var ID         Alpha 3

select virtual "C" 1's Number           Num 2
select virtual "D" 26's Number          Num 2
select virtual "E" 676's Number         Num 2


The record suffix will look like this:
update C  (1's Number)        Expr: A-1 MOD 26
update D  (26's Number)       Expr: ((A-(C+1)) MOD 676) / 26
update E  (676's Number)      Expr: A-((D*26)+C+1) / 676

update B  (Magic Var ID)      Expr: IF(E=0,' ',Chr(65+E)) & IF(D=0 AND E=0,' ',Chr(65+D)) & Chr(65+C)




regards
Frank Van Herreweghe


Category
CategoryHowTo

There are no comments on this page. [Add comment]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki trunk
Page was generated in 0.0610 seconds