Newb question: Append to a variable
John Minton
Posts: 23
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
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
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
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
Post Edited (Dave Hein) : 7/29/2010 11:43:11 PM GMT
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!
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)