Shop OBEX P1 Docs P2 Docs Learn Events
Question about embedding variable in a string statement — Parallax Forums

Question about embedding variable in a string statement

Don MDon M Posts: 1,652
edited 2013-05-06 13:16 in Propeller 1
I could swear I have seen some tricky way of embedding a variable in a string statement but can't find an example of it.

What I'm wondering is can I structure this statement in a way to make it work?

Let's say I have a byte variable called "line".

I want to print a statement like this: "Stopped at line *decimal value of line here* before continuing"

term.str(string("Stopped at line " ? "before continuing"))

Thanks.

Comments

  • localrogerlocalroger Posts: 3,451
    edited 2013-05-04 15:49
    The string operator returns the address at which the string literal resides -- in RAM, so it can be modified. So you can do something like this:
      ptr := string("Value: $")
      byte[ptr + 7] := "3"
      vid.str(ptr)
    

    ...prints "Value: 3" To do a multi-byte decimal value you would need a version of the "dec" function found in most video I/O objects (where it doesn't belong anyway) modified to write to RAM instead of spitting the bytes out to an interface. You can find one of those in my PropCMD object in the obex.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-04 19:09
    I usually just use:
    term.dec(string("Stopped at line "))  
      term.dec(x)
      term.str(string(" before continuing"))
    

    Of am I missing something in what you want to do?

    I'm pretty sure someone has an object that lets you output strings with embedded variables but I don't recall what it's called and I've never tried it. I just remembering someone saying they can't believe people resort to doing what I just posted.

    If you want to do it the way localroger suggests, I'm pretty sure I have a dec method that writes the ASCII characters of a value to a location in RAM. If you'd like to do it that way let me know and I'll look for the method. If you're output a ASCII characters to RAM, it usually makes things easier if you can have the number be a fixed length. Hex is more conducive to this kind of manipulation since we're used to seeing hexadecimal values with leading zero.

    I believe there might be an object in the library to do this. "Numbers" maybe?
  • Don MDon M Posts: 1,652
    edited 2013-05-04 19:16
    Duane Degn wrote: »
    I usually just use:
    term.dec(string("Stopped at line "))  
      term.dec(x)
      term.str(string(" before continuing"))
    

    Of am I missing something in what you want to do?

    I'm pretty sure someone has an object that lets you output strings with embedded variables but I don't recall what it's called and I've never tried it. I just remembering someone saying they can't believe people resort to doing what I just posted.

    Duane- yes I know I can do that and usually do. It's just that I thought I saw somewhere on here an example of something similar but all in the same line.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-04 19:31
    Don M wrote: »
    Duane- yes I know I can do that and usually do. It's just that I thought I saw somewhere on here an example of something similar but all in the same line.

    I think they must have used a special formatting object to do it then. Maybe someone else will remember more details.
  • RforbesRforbes Posts: 281
    edited 2013-05-05 17:56
    Hi Don,

    Here is one of the objects Mike G. uses in his HTTPServer app. This object does exactly what you're asking and a couple other nice things.

    http://code.google.com/p/spinneret-web-server/source/browse/trunk/MultiSocketServer_MikeG/StringMethods.spin

    Once I start this object, I use a simple method to work with buffers.
    pub Numstr(data,buffer)|ptr
    {
     Convert a numeric value to ascii characters and place them in the specified
     buffer at the first available position in the buffer.
    The buffer must be large enough to handle the characters we're about to add.
    
      Call usage:  Numstr(@Mylong,@databuffer)  (use the @ symbol)
    
    }
    
      ptr:=buffer+strsize(buffer)  'Look at the buffer, see how long it is and set ptr to the next available position.
      str.ToString(data, ptr)      'Call ToString and stick the integer value in location data at location ptr.
      return strsize(buffer)       'return our final string size.
    

    I'd imagine there are some much fancier ways to do this, but I really do like Mike G's object. It's spifforiffic.
  • cavelambcavelamb Posts: 720
    edited 2013-05-05 18:18
    localroger wrote: »
    The string operator returns the address at which the string literal resides -- in RAM, so it can be modified. So you can do something like this:
      ptr := string("Value: $")
      byte[ptr + 7] := "3"
      vid.str(ptr)
    

    ...prints "Value: 3" To do a multi-byte decimal value you would need a version of the "dec" function found in most video I/O objects (where it doesn't belong anyway) modified to write to RAM instead of spitting the bytes out to an interface. You can find one of those in my PropCMD object in the obex.

    I haven't tried this but why not?
    ptr := string("Value: $")   
    byte[ptr + 7] := byte(value) +48  'make ASCII?   
    vid.str(ptr)
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-05 18:23
    cavelamb wrote: »
    I haven't tried this but why not?
    ptr := string("Value: $")   
    byte[ptr + 7] := byte(value) +48  'make ASCII?   
    vid.str(ptr)
    

    That's only good if the variable value is between 0 and 9 (inclusive). Plus it doesn't meet the "single line of code" requirement.
  • cavelambcavelamb Posts: 720
    edited 2013-05-06 00:56
    Duane Degn wrote: »
    That's only good if the variable value is between 0 and 9 (inclusive). Plus it doesn't meet the "single line of code" requirement.

    WHAT single line of code requirement???
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-06 03:35
    cavelamb wrote: »
    WHAT single line of code requirement???

    See post #4.

    Sorry, blame Don.
  • cavelambcavelamb Posts: 720
    edited 2013-05-06 13:16
    Duane Degn wrote: »
    See post #4.

    Sorry, blame Don.

    Sorry. Yeah, I was responding to LocalRangers idea but trying to insert a variable
    rather than a literal.

    DON! It's YOUR fault! (Duane told me to say that :) )
Sign In or Register to comment.