Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Broseph Radson on January 18, 2011, 05:37:08 pm

Title: Help parsing a string
Post by: Broseph Radson on January 18, 2011, 05:37:08 pm
Hai im back with another probably dumb question. So i have a users input stored to I, then the first letter of I is stored to N. I have "RLDQ" stored to C. Im trying to figure out how to step thru the letters in C and compare them to N. Inside a For( loop, i have this code:
Code: [Select]
If (N=(C+A)):Goto KK:End Where A increments from 1 to 4. It seems to always return false and i know it has to do with the If block. Help plz :) EDIT: i just realized that when i copy the first byte of I to N, it doesnt stay as a char. Oops
Title: Re: Help parsing a string
Post by: jnesselr on January 18, 2011, 05:49:38 pm
I would also do A from 0-3, not 1-4.  Also, {C+A} I believe would be more accurate as you are getting the byte at location C+A.
Title: Re: Help parsing a string
Post by: Runer112 on January 18, 2011, 05:54:09 pm
May I suggest a simpler soultion? inData(N,C) will return the 1-based position at which the byte stored in N is found in the string, or 0 if it is not found.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 18, 2011, 05:59:55 pm
Ok i cant seem to get the first byte of I as a character so ill try inData. Will report back in a sec. One sec later, I got this to work:
Code: [Select]
For(A,0,3):If inData({C+A}, I):Goto KK:End:End Thatx guise! I didnt know about inData lol
Title: Re: Help parsing a string
Post by: AngelFish on January 18, 2011, 06:57:06 pm
inData( would be better. Remember that you'll occasionally have problems with getting the characters to display properly, so set the byte with a known hex value to test it. Don't trust your input routine.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 18, 2011, 07:16:53 pm
Ok another question. How to i get a substring? Like get the last part of a string?
Code: [Select]
For(A,3,Length([string]))
//something to get the character at A and append it to another string
End
Or is there a command that does this?
Title: Re: Help parsing a string
Post by: Quigibo on January 18, 2011, 07:33:17 pm
To get the end of the string you don't even need a command.  If your string is Str1, lets say it says: "Hello".  Then Str1+1 is "ello", Str1+2 is "llo", Str1+3 is "lo" etc.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 18, 2011, 07:49:40 pm
orly i didnt try that. ill try once i remake the program. Just got a RAM clear messing with interrupts lol. which reminds me i have a question about interrupts but ill make a new thread for that.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 19, 2011, 08:52:15 pm
Another question involving strings (since i cant for the life of me find the thread where this was mentioned), how do i insert a token at the beginning of a string? Like if R is "ABC" and i want to make it say "prgmABC"
Title: Re: Help parsing a string
Post by: Deep Toaster on January 19, 2011, 09:06:44 pm
The easiest Axe way to do it would be to make a new string and copy it over.

If you want a faster way to do it, there's bcall(_InsertMem), which allocates extra memory for a variable.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 19, 2011, 09:26:48 pm
ok that worked but the token is copying as a character. How do i copy a token? It gives me a bad symbol error when i do >Tok in the copy and when i try to store it to its own string
Title: Re: Help parsing a string
Post by: Deep Toaster on January 19, 2011, 11:02:31 pm
Use T. For example, Tprgm→{getCalc("Str0")}.

Another question involving strings (since i cant for the life of me find the thread where this was mentioned), how do i insert a token at the beginning of a string? Like if R is "ABC" and i want to make it say "prgmABC"

How can R be a string?
Title: Re: Help parsing a string
Post by: Broseph Radson on January 19, 2011, 11:16:45 pm
Good question but it works for me. I cant store input to a regular string var so idk. I forgot about that T thing thanks. The most recent version of the commands list i have printed (no computer access) is 0.4.1 so i guess it wasnt listed in that one
Title: Re: Help parsing a string
Post by: Deep Toaster on January 19, 2011, 11:17:46 pm
DJ posted an online version here (http://ourl.ca/8409/154963).
Title: Re: Help parsing a string
Post by: Broseph Radson on January 20, 2011, 10:33:52 pm
well i tried copying the prgm token to the string then copying the progname to the string after the prgm token, but when i display it it spits out garbage. im sorry i know i really suck at this
Title: Re: Help parsing a string
Post by: Deep Toaster on January 20, 2011, 10:35:39 pm
well i tried copying the prgm token to the string then copying the progname to the string after the prgm token, but when i display it it spits out garbage. im sorry i know i really suck at this

Can you post your copying and displaying code together? Someone may be able to see what went wrong.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 20, 2011, 10:44:24 pm
ok i can try. my phone doesnt do hard returns though.
Code: [Select]
input->I:Tprgm->{R}:I->{R+1}:Disp R Input wont store to a string pointer and i dont want to use OS variables unless i absolutely have to.
Title: Re: Help parsing a string
Post by: Deep Toaster on January 20, 2011, 10:45:03 pm
Um, what's R again? What does it point to?

If you never set it (like with GetCalc("Str0")→R) R could point to any address in memory. Most likely R is 0, which points to a location in the flash memory. You're not allowed to store anything to flash, so when you do Tprgm→{R}, it doesn't do anything.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 20, 2011, 10:47:25 pm
The completed string which i want to be "prgmWHATEVS". EDIT: tried doing Tprgm->R instead of {R} and it displayed different garbage.
Title: Re: Help parsing a string
Post by: Deep Toaster on January 21, 2011, 09:26:25 am
The completed string which i want to be "prgmWHATEVS". EDIT: tried doing Tprgm->R instead of {R} and it displayed different garbage.

You're using R as a pointer when you store it as {R}. In other words, the variable R should already be holding an address in memory where you want to store it, or else you're storing to any random spot in memory.

If you just do Tprgm->R, you're setting the address to the value of Tprgm, which is $5F, which is still a random spot in memory. In fact, it still points to flash, and so it's still not doing anything.

The thinng is, R isn't actually a variable. It's just two bytes at a specific spot in memory. When you do {R}, you're taking the value of R and storing to that address. So if R were 32768, {R} would be the byte stored at the 32768th byte in memory. You can't just randomly store to {R} and expect it to be at a safe place.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 09:38:08 am
Code: [Select]
GetCalc("Str0")->A
GetCalc("Str1")->R
[sup]T[/sup]prgm->R
input->A
A->{R+1}
Disp R

Doesnt display properly, but Str1 does have the token in it. The input appears to not store the input as a string...
Title: Re: Help parsing a string
Post by: Deep Toaster on January 21, 2011, 10:34:21 am
Oh, you do init R, never mind then :)

But when you do input→A, you're only storing the pointer to the inputted string to A. When you do A→{R+1}, you're storing this value to the string (actually only the smaller byte of it). You need to replace the line A→{R+1} with Copy(A,R+1,LENGTH) and it should work. Make sure Str1 is at least LENGTH+1 long, though.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 10:38:50 am
Derp moment there. Forgot to give Str1 a size. RAM clear lol. 1 more try
Code: [Select]
GetCalc("Str0",9)->R
GetCalc("Str1",8)->A
input->A
[sup]T[/sup]prgm->R
Copy(A,R+1,8)
Disp R
Gives the following output:
■■■#F2.43

Also Str1 is a bunch of question marks
Title: Re: Help parsing a string
Post by: Deep Toaster on January 21, 2011, 11:34:00 am
Tprgm->R

Why that? You're storing the value as an address again, not to the address.

Replace it with Tprgm->{R}.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 11:46:47 am
I am so fail at this lol. testing...
Title: Re: Help parsing a string
Post by: Runer112 on January 21, 2011, 02:30:15 pm
Is there any reason you're using the OS string variables for this? It might be easier to just use your own data storage. Also, prgm is a token, not a character, and would not display correctly as part of an ASCII string. May I suggest something like the following?

Code: [Select]
.Prepare a string buffer
"prgm"Zeros(9)→Str1
.Fill the string buffer with input
Copy(input,Str1+1,8)
.Display prgm token
Disp ᵀprgm►Tok
.Display program name
Disp Str1+1

Str1 will also contain the correct data for any GetCalc() operations that you may want to perform after this.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 04:04:34 pm
Wow thats cool i didnt know input could be used like that. It works thanks!
Title: Re: Help parsing a string
Post by: AngelFish on January 21, 2011, 04:12:14 pm
I'm not sure exactly what you're doing, but if you need something very adaptable and want to define the keys, you could always use the more complicated method I used for command line:

1) Record Keypress
2) Determine if Keypress is within valid range
3) Search for keycode in lookup table
4) Use data associated with lookup table to look up the input token in another table
5) Write data to known location
6) Repeat steps 1-6 until user presses Enter
7) Write termination data to the end of the location
8) Step through string until a match is found within parser commands
9) Jump to location of executable code associated with parser command
10) Execute data
11) Return to step 1
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 04:21:15 pm
I am a fan of graphscreen input, but until i can memorize the math to do a lookup, its a bit out of my range. I think i can find out though. I believe someone posted how to do that somewhere
Title: Re: Help parsing a string
Post by: AngelFish on January 21, 2011, 04:24:26 pm
Actually, it's surprisingly easy in Axe. I cheat and use the inString command, where the string to be searched contains the hex keycodes of all of the keys used.
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 05:40:14 pm
Oh cool how?? Axe is amazing
Title: Re: Help parsing a string
Post by: AngelFish on January 21, 2011, 05:53:21 pm
Er, let me dig up my source.

Okay, here's some example code. I'd post the original code, but it's very complex because I'm dealing with a lot of commands (and I also have a few 1500 byte strings laying around). Str1 is the key lookup table and defines all of the alphabet as well as the space button and the "." button. L2 holds the character set, although only not all of the keys defined in the lookup table have characters defined.

Code: [Select]
:.Lookup table
:[2F271F2E261E160E2D251D150D2C241C140C2B231B130B2A221A211911]→Str1
:.SET UP character TABLE
:For(A,65,90
:.SET LETTERS
:A→{L2+A-65}
:End
:.SET SPACE
:41→{L2+26}
:1→N→P+40→X→Y
:15→J+2→I
:.RECORD KEYPRESS
:getKey→K
:.REPLACE GRAPHICS ERASED BY    MOUSE
:sub(RCL
:If K>0
:.SEARCH LOOKUP TABLE
:!If K-56
:If P-1
:sub(BCK
:End
:Goto 01
:Else
:inString(K,Str1)→A
:End
:.IF KEYPRESS ISVALID
:If A
:If I+48<N
:1→N
:O+6→O
:End
:Text(I+N,J+O,L2+A-1►Tok
:{L2+A-1}→{L5+P}
:.MOVE CURSOR
:N+5→N
:P+1→P
:End
:End

The parser itself is kind of a mess though and over a thousand bytes of code. I'll let you figure it out :P
Hint: You just step through the string starting from L5.

There are also a lot of optimizations I haven't made yet, so...
Title: Re: Help parsing a string
Post by: Broseph Radson on January 21, 2011, 05:58:46 pm
Looks cool ill try it out soooooooooon :)