Shop OBEX P1 Docs P2 Docs Learn Events
Is there a better way to update a string than this? — Parallax Forums

Is there a better way to update a string than this?

idbruceidbruce Posts: 6,197
edited 2011-01-28 05:37 in Propeller 1
llllllllllllllllllllll

Comments

  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 11:02
    You might want to look at the STRSIZE and BYTEMOVE inbuilt functions in the Propeller manual.
    You'll need to be sure the TxString array is big enough to hold the incoming strings.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 11:05
    Heater

    Application specific.... The string can be no longer than 21 characters.

    Bruce
  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 11:08
    By the way. If you ever want to run a repeat loop over a string for whatever reason it's better to get the length into a variable, say "slen" and then do a repeat loop that many times. Because as it stands you are calling STRSIZE every time around the repeat and STRSIZE will loop around the string every time counting the bytes.

    Edit: ERROR. The above is wrong. The repeat expression is only evaluated once. Sorry. See later post.
  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 11:09
    OK so just get the length with STRSIZE and move the bytes with BYTEMOVE.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 11:10
    Heater

    That makes perfect sense to me. Thanks for the tip.

    Bruce
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-27 11:15
    You'll need to BYTEMOVE STRSIZE+1 bytes to include the zero terminator, in case the receiving string doesn't have one, or if it's located somewhere else.

    -Phil
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 11:15
    Heater

    Yea, that works perfectly and looks much better.

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 11:16
    Phil

    Thanks for that tip, I am sure it will come in handy in the future.

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 11:20
    Phil

    Got it thanks
    Bruce
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-01-27 12:56
    Heater. wrote: »
    By the way. If you ever want to run a repeat loop over a string for whatever reason it's better to get the length into a variable, say "slen" and then do a repeat loop that many times. Because as it stands you are calling STRSIZE every time around the repeat and STRSIZE will loop around the string every time counting the bytes.
    This is not true. If you use the REPEAT <Count> method, it only evaluates <Count> once (See Page 190 of Manual v1.1). So storing the string length in a variable will actually slow down the overall process (by about 360 cycles).
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-27 14:18
    Bobb Fwed wrote: »
    This is not true. If you use the REPEAT <Count> method, it only evaluates <Count> once (See Page 190 of Manual v1.1). So storing the string length in a variable will actually slow down the overall process (by about 360 cycles).
    Yes, "repeat strsize(str)" will initialize the loop count at the beginning. If strsize is zero it will not execute the loop.

    Now if you did "repeat i from 1 to strsize(str)" it would call strsize at the end of each loop. And if the value of strsize was zero it would actually execute the loop twice, with values of 1 and 0 for i.
  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 17:09
    Oops. Sorry.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 17:27
    Heater

    Shame on you for giving the newbie bad advice :)

    I forgive you though :)

    Bruce
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-27 17:32
    Isn't this (what's been hinted at) the easiest way to go here?
    [B]bytemove[/B](@TargetString, @SourceString, [B]strsize[/B](@SourceString)+1)
    
  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 17:48
    idbruce,

    Sorry, yes, cardinal sin.

    To make up for it, notice that if you do actually want the expression in the repeat statement to be re-evaluated each time around the loop then you can use "repeat while"

    For example in the following:
    PUB
        repeat func
           ser.str(string("Looping 1..."))
           ser.tx(13)
           ser.tx(10)
    
        repeat while func
           ser.str(string("Looping 2..."))
           ser.tx(13)
           ser.tx(10)
        repeat
    
    PUB func    
        return 3
    

    The first repeat goes around 3 times as the repeat expression is evaluated only once and is 3 after that the 3 is decremented each loop.
    The second repeat goes around forever as the expression is evaluated every time and the while condition keeps getting reset to 3. As it were.

    This is TRUE. I tested it:)
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 17:49
    Hello Jon

    Yes, and that was heater first suggestion without the + 1, and then Phil came in and added a comment about the +1. That is a lot neater than my first way of doing it.

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-27 17:54
    Heater

    I am glad I asked this question, it was nice, just to find a better way, but finding out more with your test loop was also cool.

    Bruce
  • Heater.Heater. Posts: 21,230
    edited 2011-01-27 17:59
    Excellent. We both learned something from it.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-28 05:37
    idbruce wrote: »
    is there a better way of updating a string than this?
    PUB UpdateTxString(NewTxStringPointer) | Char, Index
      Index := 0
     
      REPEAT STRSIZE(NewTxStringPointer)
        Char := NewTxStringPointer++
        TxString[Index] := Char
        Index++
    
    e
    Bruce,

    The original code you posted wouldn't work the way you want it to. You need to use a byte[] intrinsic to access the data in NewTxStringPointer. So the code should look like this:
    PUB UpdateTxString(NewTxStringPointer) | Index
      Index := 0
     
      REPEAT STRSIZE(NewTxStringPointer)
        TxString[Index++] := byte[NewTxStringPointer++]
    

    I'm assuming TxString was defined as a byte array in a DAT or VAR section.

    Dave
Sign In or Register to comment.