Picking out characters from a string
LaserSteve
Posts: 21
in Propeller 1
I'm trying to display individual words from a string.
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OBJ pst : "Parallax Serial Terminal" var long value, x Pub Main pst.Start(115_200) waitcnt(clkfreq * 2 + cnt) pst.Clear pst.Str(String("press any key")) value := pst.CharIn pst.Chars(pst#NL,1) x:=1 repeat while x < 16 pst.Char(@three[x]) x++ pst.Char(@three[x]) x++ pst.Char(@three[x]) x++ pst.Chars(pst#NL,1) pst.Chars(pst#NL,1) pst.Str(String("done")) repeat Dat 'Three letter words Three byte "theandforarebutnotyouallanycanherwasoneouroutday",0
Comments
Changes are:
* Reference the variable 'Three' directly, instead of its address
* The index 'x' is initialized with 0 (that's the first element of your variable 'Three' - it started with 1 before, which would be the 'h' in the word 'the')
* The repeat block now loops as long as the value of three isn't 0/NUL (which was used but not tested for in your original example)
Cheers,
Jesse
I tested with this snippet and it works fine. Note that I use my own version of FDS, not PST.
BTW, when you see repeated code in your listing you can use condense it with... repeat. For example, from your original listing you could do this:
Is this the one?
http://obex.parallax.com/object/579
You guys are the best!
I fixed it by subtracting 1 from the extraction index. I just want to know why I needed to do this.
here is the word extraction subroutine
Thanks.
You were right, thanks.
Two things: 1) Use bytemove, not wordmove, and 2) Use strsize() so that you don't have to count the length of the string -- this will let you update the string without having to go back in and fix the code. The reason for the +1 is that it will copy the terminating 0 from the source to the destination.
Yes, that's it.
Kye has a very nice way of commenting his code, especially tuned for beginners.
To me, the names of the routines and variables are quite long, but they do exactly what the name says.