Author Topic: Using tokens and strings together  (Read 3327 times)

0 Members and 1 Guest are viewing this topic.

Offline Emerov

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 11
  • Rating: +0/-0
    • View Profile
Using tokens and strings together
« on: April 20, 2012, 03:07:40 am »
I'm attempting to access arbitrary OS vars from the calculator, and subsequently delete them. The code works with programs, groups, appvars, and letters, but whenever I try to access the vars represented by tokens, it does not work properly. Basically, I copy the randomly selected var name over into another string so that I can delete it, but the string doesn't seem to recognize the tokens following the first one. Here is my code (simplified):
Code: [Select]
.DELSUB2
Fix 5
"var "->Str3
"ABCDEFGHIJKLMNOPQRSTUVWXYZØ"->Str5
"L1L2L3L4L5L6"->Str6
"  "->Str8
sub(LSD)
Return

Lbl LSD                             //This will only ever delete L1. If the randomly selected number is not 0, then nothing will happen
Copy(rand^6*2+Str6,Str8,2)
DelVar Str8
Return

Lbl ABC                             //This works
Copy(rand^27+Str5,Str3+1,1)
DelVar Str3

Is there a way to get around this lack of string support for tokens? Like, can these variables be accessed via hex code or something? Thank you!
« Last Edit: April 21, 2012, 05:30:23 pm by Emerov »

Offline Emerov

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 11
  • Rating: +0/-0
    • View Profile
Re: Using tokens and strings together
« Reply #1 on: April 21, 2012, 05:38:52 pm »
*Bump* Can somebody help me out with this?

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Using tokens and strings together
« Reply #2 on: April 21, 2012, 06:00:19 pm »
It seems that you are operating under the assumption that each list name in the string takes up 2 bytes, but they actually take up 3. The tokens themselves are only 2 bytes, but their names in the VAT are 3 bytes. Tricky, I know. But make Str8 one character longer and adjust the Copy() a bit and you should be all set.



On another note, I'll mention a few tips you could use to make this code smaller. Firstly, the letters and list names are sequential values, so there's no need to look them up from a string. You can just add an offset to the base letter/list name. Secondly, if you're only moving 1 or 2 bytes, just use a normal store, no need to use Copy(). :P

Code: [Select]
.DELSUB2
Fix 5
"var "→Str3
"L₁"→Str8
sub(LSD)
Return

Lbl LSD
rand^6→{2+Str8}ʳ
DelVar Str8
Return

Lbl ABC
rand^27+'A'→{1+Str3}ʳ
DelVar Str3
Return
« Last Edit: April 21, 2012, 06:47:44 pm by Runer112 »

Offline Emerov

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 11
  • Rating: +0/-0
    • View Profile
Re: Using tokens and strings together
« Reply #3 on: April 21, 2012, 06:41:59 pm »
Awesome, it works and makes sense now! :)

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Using tokens and strings together
« Reply #4 on: April 21, 2012, 06:48:46 pm »
Oops, I just noticed a fatal mistake with my optimization to Lbl ABC; I forgot to add 'A' to the random result to actually make the random result in the range of letters! I fixed it now, so if you already grabbed that code, you should grab the fixed version.
« Last Edit: April 21, 2012, 06:49:49 pm by Runer112 »

Offline Emerov

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 11
  • Rating: +0/-0
    • View Profile
Re: Using tokens and strings together
« Reply #5 on: April 21, 2012, 08:30:37 pm »
Oh, I just added E41 and it worked fine. Is the 'A' thing better to use or does it just convert it during parsing?
« Last Edit: April 21, 2012, 08:33:04 pm by Emerov »

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Using tokens and strings together
« Reply #6 on: April 22, 2012, 12:24:59 am »
They're the same thing. The 'char' notation is more of a convenience/readability feature. The same applies for token values, which can be inserted with Ttoken.
« Last Edit: April 22, 2012, 12:26:35 am by Runer112 »