# Omnimaga

## Calculator Community => Major Community Projects => The Axe Parser Project => Topic started by: Okimoka on December 13, 2016, 01:06:17 pm

Title: Dynamically access variables
Post by: Okimoka on December 13, 2016, 01:06:17 pm
Is there a way to shorten this code?

Code: [Select]
`If L=1 Pt-On(...,Pic1)EndIf L=2 Pt-On(...,Pic2)EndIf L=3 Pt-On(...,Pic3)End...`
I tried accessing the images using memory adresses but it doesn't seem like the Pic variables have constant memory space between them.
Is there a way to evaluate an expression as a variable?
Is this already the most efficient you can get?
Title: Re: Dynamically access variables
Post by: Sorunome on December 13, 2016, 01:09:56 pm
Well, you could do something like
Code: [Select]
`Pt-On(....,L=1?Pic1,(L=2?Pic2,(L=3?Pic3))))`Or, preferable, probably a pointer-table, however it slipped my mind right now how to do those in axe....
If you are wondering what that question-mark stuff does: https://derpybot.sorunome.de/axe.php?q=%3F (https://derpybot.sorunome.de/axe.php?q=%3F)
Title: Re: Dynamically access variables
Post by: Okimoka on December 13, 2016, 01:35:49 pm
Very nice, the ternary solution is already so much cleaner!
If I understood right, a pointer-table would require me to write the adresses of all Pic variables into a Buffer first?
Title: Re: Dynamically access variables
Post by: Runer112 on December 13, 2016, 01:48:58 pm
You can do this by writing the addresses of the three sprites into a buffer, yes. The code would look something like this:

Code: [Select]
`Pt-On(X,Y,{L*2-2+Data(Pic1ʳ,Pic2ʳ,Pic3ʳ)}ʳ)`
However, best would be to declare the sprite data in sequence so each one's data immediately follows the next, which allows this:

Code: [Select]
`[0123456789ABCDEF]→Pic1[FEDCBA9876543210]→Pic2[FFFFFFFFFFFFFFFF]→Pic3...other stuff here...Pt-On(X,Y,L*8-8+Pic1)`
And a suggestion: in either case, your life would be made easier by using zero-based numbering. That generally translates into simpler/more efficient code. In the first case, you could remove the -2, and in the second case, you could remove the -8.
Title: Re: Dynamically access variables
Post by: Okimoka on December 13, 2016, 03:40:37 pm
Works fantastically, thank you both so much for your time!
I revised the program to use zero-indexing, good hint  :thumbsup:
Title: Re: Dynamically access variables
Post by: Sorunome on December 13, 2016, 03:48:19 pm
You can do this by writing the addresses of the three sprites into a buffer, yes. The code would look something like this:

Code: [Select]
`Pt-On(X,Y,{L*2-2+Data(Pic1ʳ,Pic2ʳ,Pic3ʳ)}ʳ)`
However, best would be to declare the sprite data in sequence so each one's data immediately follows the next, which allows this:

Code: [Select]
`[0123456789ABCDEF]→Pic1[FEDCBA9876543210]→Pic2[FFFFFFFFFFFFFFFF]→Pic3...other stuff here...Pt-On(X,Y,L*8-8+Pic1)`
And a suggestion: in either case, your life would be made easier by using zero-based numbering. That generally translates into simpler/more efficient code. In the first case, you could remove the -2, and in the second case, you could remove the -8.
You actually only have to name the first block, so that your code looks like this (assuming now you have zero-based numbering):
Code: [Select]
`[0123456789ABCDEF]→Pic1[FEDCBA9876543210[FFFFFFFFFFFFFFFF...other stuff here...Pt-On(X,Y,L*8+Pic1)`