Shop OBEX P1 Docs P2 Docs Learn Events
Newb question: Append to a variable — Parallax Forums

Newb question: Append to a variable

John MintonJohn Minton Posts: 23
edited 2010-07-31 17:53 in Propeller 1
Hello,

I have a pretty basic question regarding spin.

I have multiple variables set and would like to set them in order into another variable.

Example:

CON
_clkmode = xtal1 + pll16x ' Feedback and PLL multiplier
_xinfreq = 5_000_000 ' External oscillator = 5 MHz

VAR
byte var1, var2, var3
long varall

PUB main
var1 := "1"
var2 := "2"
var3 := "3"

varall := var1.var2.var3."some text here"
'End

I am a native PHP programmer, so normally for me its like this:
<?php
$var1 = "My ";
$var2 = "name ";
$var3 = "is ";

$varall = $var.$var2.$var3."John";

echo $varall; // print to screen, "My name is John"

?>

... As you can see, the "." in between variables glues them together.

Any suggestions on how I can accomplish this in spin?

I know that some of the code in my sample is wrong...but I am sure you can understand my specific question.

Thanks in advance!!

Post Edited (John Minton) : 7/29/2010 11:14:49 PM GMT

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-07-29 23:21
    There are no string variables in Spin. You can use an array of bytes. Assuming you have properly-defined z-strings you might do something like this:

    bytefill(@varall, 0, 32) ' varall can hold 32 characters
    bytemove(@varall, @var1, strsize(@var1))
    bytemove(@varall+strsize(@var1), @var2, strsize(@var2)
    bytemove(@varall+strsize(@var1)+strsize(@var2), @var3, strsize(@var3)
    



    Sorry, I don't do much in the way of string-manipulation with my projects so this is probably terribly inelegant.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-29 23:22
    varall[noparse][[/noparse] 0 ] := var1 ' copies the first byte
    varall[noparse][[/noparse] 1 ] := var2 ' copies the second byte
    varall[noparse][[/noparse] 2 ] := var3 ' copies the third byte
    bytemove(@varall+3,string("some text here"),15) ' copies the string and the zero byte at the end

    Note: This assumes that the 3 byte values are just that ... single characters. JonnyMac's suggestion is for when these are zero-terminated strings.
    Remember that there are no string variables in Spin, only byte arrays ... and the maximum length of the byte array has to be declared (including
    the zero byte at the end).

    Post Edited (Mike Green) : 7/29/2010 11:26:54 PM GMT
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-07-29 23:38
    So just to add to the previous comments, varall must be large enough to hold the concatenated string.· Your PHP example would look like this in Spin

    OBJ
      ser : "FullDuplexSerial"
     
    VAR
      long var1
      long var2
      long var3
      byte varall[noparse][[/noparse]80]
     
    PUB main
      ser.start(31, 30, 0, 57600)
     
      var1 = string("My ")
      var2 = string("name ")
      var3 = string("is ")
     
      bytemove(@varall, var1, strsize(var1) + 1)
      bytemove(@varall + strsize(@varall), var2, strsize(var2) + 1)
      bytemove(@varall + strsize(@varall), var3, strsize(var3) + 1)
      bytemove(@varall + strsize(@varall), string("John"), 5)
     
      ser.str(@varall)
     
    


    Post Edited (Dave Hein) : 7/29/2010 11:43:11 PM GMT
  • John MintonJohn Minton Posts: 23
    edited 2010-07-31 17:52
    Thank you everyone for your input!

    I am able to get some gibberish looking info in the serial terminal!

    I'll be looking to convert that serial info to legible words next.

    ...I think I am getting the hang of Spin. It's not so bad once you understand a few basics!
  • John MintonJohn Minton Posts: 23
    edited 2010-07-31 17:53
    One last thing, in case anyone else uses the code Dave provided, a small correction (var1-3 needed operator ":" before the "="):


    OBJ
    ser:"FullDuplexSerial"

    VAR
    long var1
    long var2
    long var3
    byte varall[noparse][[/noparse]80]

    PUB main
    ser.start(31, 30, 0, 57600)
    waitcnt(clkfreq+cnt) ' wait a second to give time to enable serial terminal
    var1 := string("My ")
    var2 := string("name ")
    var3 := string("is ")

    bytemove(@varall, var1, strsize(var1) + 1)
    bytemove(@varall + strsize(@varall), var2, strsize(var2) + 1)
    bytemove(@varall + strsize(@varall), var3, strsize(var3) + 1)
    bytemove(@varall + strsize(@varall), string("John"), 5)

    ser.str(@varall)
Sign In or Register to comment.