Shop OBEX P1 Docs P2 Docs Learn Events
String manipulation — Parallax Forums

String manipulation

James NewmanJames Newman Posts: 133
edited 2007-12-10 00:29 in Propeller 1
I've got a section of code that isn't working as it should. Can anyone spot my problem? The following is the offending code, it should allow numeric characters to be appended to a string, and backspace should remove the last character of the string.

I'm using the standard keyboard object, and inputString is defined in the Var section as "long inputString[noparse][[/noparse] 2 ]"

key := keyboard.key
case key
          "0".."9": i := 0                                                     'Numbers 0-9
                    repeat _MAX_INPUT*4-1
                      if byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] == 0
                        byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] := key
                        quit
                      i++
          $C8: i := 0                                                          'Backspace
               repeat _MAX_INPUT*4
                 i++
                 if byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] == 0
                   byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i-1 ] := 0
                   quit




Ohhh and i'm displaying it via FloatString objects .str. When I do, I get garbage.

Post Edited (James Newman) : 12/9/2007 8:57:06 AM GMT

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-12-09 02:48
    (1) Please use a blank after each opening bracket [noparse][[/noparse] to avoid the tagging sequences for bold, italic, etc.

    (2) The main question is: Where is the memory for your string?
    When you say: "LONG inputString" this is only part of the truth...
    (a) if you assign the address of the string-buffer (but where is it?) to this variable, you obviously have to use
    BYTE[noparse][[/noparse]inputString]
    


    If you - erronoiusly - use the LONg as string-buffer, that your code could work, but you are limited to 3 characters.

    I think you are not fully aware that you have to allocate memory for any "string" you use.

    (3) Make shure the string-buffer is cleared each time time you start anew.
  • hippyhippy Posts: 1,981
    edited 2007-12-09 02:50
    You'll probably want "byte [noparse][[/noparse] @inputString + i ]" and don't want to be resetting "i:=0" on every key press.

    To be honest, it's not clear from the code what you intend it to be doing.
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-09 02:59
    @hippy:
    BYTE[noparse][[/noparse]pointer][noparse][[/noparse] i ] [noparse][[/noparse]code] is fine as well, as more readeable for WORDs and LONGs
    [noparse][[/noparse]code]i:=0
    

    is quite all right, too smile.gif
  • James NewmanJames Newman Posts: 133
    edited 2007-12-09 08:54
    hippy> I've actually tried that too, got the same/similar results. I do intend to clear i each keypress, I search through the string at each keypress and then append, or remove. That's actually what I'm trying to do. It seems to me that if the string contains all null bytes, then I should be able to search for the first one, set it to a byte, then 'quit' out of the loop. This should append to the string, so long as I don't remove the last null byte. The same idea is being used for backspace, I search for a null byte, then set the byte before it to null also.... Or atleast this was the way it was supposed to work... I havn't had luck so far :/

    deSilva> The string is allocated as a long in the var section, and is 2 longs in size

    Edited the above code so that it looks better, ohhh and _MAX_INPUT is the amount of -longs- allocated for the string, 2 atm.

    Post Edited (James Newman) : 12/9/2007 8:59:12 AM GMT
  • James NewmanJames Newman Posts: 133
    edited 2007-12-09 09:40
    EDIT: Well thought I did... crumbles when I get to the 5th character, even if I increase the size of the 'array' inputString.... Well I'm off to bed...

    Ohhh, got it.

    Thought about it, and I shouldn't have the '@' symbols. Without them, it works... I had tried without before, but at that time some other things where different.

    Well, thanks anyway, I'll be back when I hit my next stumble.

    Post Edited (James Newman) : 12/9/2007 9:54:21 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-09 14:06
    Good to hear! Do you have any idea why I posted this yesterday at 6:48? It was meant to help you... Please read answers given to your requests.
    Thank you.
  • James NewmanJames Newman Posts: 133
    edited 2007-12-09 22:42
    deSilva> Did, which is why I replied specifically to them... they however didn't help. I did get something working, but only the first 4 letters. After the first 4, everything goes bad. I set the inputString variable to 'long inputString[noparse][[/noparse]10]' which should be enough space for 39 characters, yet it still fails on the 5th.

    If I can't figure it out soon, I'll make a full program that shows the problem,and post it. Thanks in advance.


    EDIT: Ok, well finally got it figured out. I went back and declared the inputString variable as a byte array insead of a long. Declaring it as a long had given me working results because when I went to draw the string, I wasn't passing the address of inputString, I was passing the value (which would have been the first character). The only reason this worked is because in one test I had put 'inputString = string("Test"). This ofcourse was an error because it was assigning the address of 'string("Test") to the first long in 'inputString'. Which allowed display.str to work since it requires an address. I had tried several methods for changing the string, and most where valid, the problem was infact mostly in my displaying of the string.

    Well, that code wasn't posted, so you all didn't have much of a chance to help me tongue.gif I would have posted that, but it's pretty complicated because I've written my own string drawing 'helper' routines.

    deSilva> Just wanted you to know that I did read everything in the thread, problem was that I had tried everything mentioned, or was already aware of it. Had I posted all of the code, someone might have seen my problem, but it has alot of asm mixed in, and would have complicated things more. Thanks for trying to help though, maybe next time I'll have all the information posted. tongue.gif Probley won't happen though, as I rarely post problems with programming. The only reason I posted this was because I'm on a tight dealine.

    Anyway, problem solved, everything above is pretty much useless. I was doing 'display.str(inputString)' instead of 'display.str(@inputString)'.

    Post Edited (James Newman) : 12/9/2007 11:31:11 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-10 00:29
    @James
    There were many possibilities how you could have defined your string buffer, if any.

    But it is good to hear you now understand the way adressing and dereferencing works. That is of fundamental importance for any program of intermediate complexity...
    I am not sure wehat you describe as a "mistake" is really wrong. Some of the things you mention are quite valid, but have to be continued consequently...
Sign In or Register to comment.