cancatenation of variables
lfreeze
Posts: 174
I need some help with what I think is a simple programming question. I have read the stamp manual and searched for posts on this subject,
but have been unable to find anything.
I am trying to put together two variables to create a new variable.
Example
Variable_1 = 11
Variable_2· = 22
Variable_3=1122
I want to create the third new variable from variable_1 and Variable_2, that will = 1122
I looked at different arithmetic functions and arrays but can't seem to find a way to accomplish this.
Any help would be greatly appreciated.
Larry
·
but have been unable to find anything.
I am trying to put together two variables to create a new variable.
Example
Variable_1 = 11
Variable_2· = 22
Variable_3=1122
I want to create the third new variable from variable_1 and Variable_2, that will = 1122
I looked at different arithmetic functions and arrays but can't seem to find a way to accomplish this.
Any help would be greatly appreciated.
Larry
·
Comments
There is no catenation operator, per se, but if you know the data type, and limits of Variable_1 and Variable_2, here is one way to do it:
Variable_1 VAR BYTE
Variable_2 VAR BYTE
Variable_3 VAR WORD
Variable_1 = 11
Variable_2 = 22
Variable_3 = 100 * Variable_1 + Variable_2
DEBUG Variable_3
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Thanks for the response. I do not know the limits of variable 1 or variable 2. So multiplication will work with the example I provided,
but would not work with other combinations. I think the questions would be better defined if I had used alpha characters i.e
variable_ 1=ab
variable_2=cd
the desired value for variable_3 is· abcd
It seems like a simple problem, but I cannot find a solution.
Larry
Doing such things for display purposes is easy, because the various serial transmission commands provide format controls that achieve what you want. For example, I could write:
DEBUG "Variable 1=", STR Variable_1\2,", Variable 2=", STR Variable_2\2,CR
DEBUG "They concatenate to this: ", STR Variable_1\2, STR Variable_2\2, CR
Try that on a stamp. It displays:
Variable 1= AB, Variable 2= CD
They concatenate to this: ABCD
It·works, but you have to see the full code for that trivial task·to appreciate that a Stamp is not a string processor, nor is PBasic a string processing language:
' {$STAMP BS2p}
' {$PBASIC 2.5}
Variable_1 VAR Byte (2)
Variable_2 VAR Byte (2)
Variable_1(0)="A"
Variable_1(1)="B"
Variable_2(0)="C"
Variable_2(1)="D"
DEBUG "Variable 1=", STR Variable_1\2,", Variable 2=", STR Variable_2\2,CR
DEBUG "They concatenate to this: ", STR Variable_1\2, STR Variable_2\2, CR
I imagine someone who fools with strings in PBasic more could think of a better way to initialize those variables to constants, but I don't know another way off hand. But it's actually worse. Your example is pretty simple of course. To actually do the concatenation in variable space, I would have to write this code another way, but it's so long I've put it in an attachment. That version produces:
Variable 1= AB, Variable 2= CD
They concatenate to this: ABCD
and by golly, Variable 3= ABCD
But the Stamp runs out of space shortly, around string lengths of six characters, and that code doesn't even deal with realistic strings of variable length. For that, you need to use a null terminator convention·like we do in C dialects.
Mind you, all the serial interface commands I can think of do these things for you, but as a function of formatting. Not as a way to manipulate strings in the variable space of the Stamp. If all you want to do is control the appearance of displayed data, read the format operators section closely. You can do a lot with those operators.
Incidentally, I'm formatting event log entries in text for a current project, and I'm not doing any string manipulation for my own purposes. I use the format operators and simply target them to the scratchpad ram until I've built up the event report. Then I transfer the contents of the ram to an eeprom queue which is eventually transmitted as an e-mail message. That's the way I recommend you use if you can limit your app to a final string no larger than the scratchpad ram on the Stamp you're using.
Post Edited (Harbor) : 7/27/2006 5:35:38 AM GMT
Larry
Just remember that there is a "scratch pad RAM area" (minimum of 64 bytes of free RAM) that you can use on any Stamp BS-2SX or better (including the BS-2p of course). Check the GET and PUT commands in the PBASIC Stamps Reference Manual, or the PBASIC Help file. These data accesses are MUCH faster than EEPROM reads and writes; better than 2:1 times faster, by my estimates. That should help you get around any transient variable space limitations for most applications.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Thanks for the response. After looking at the GET and PUT, commands, the BS2P will give me 136 bytes of scratch pad ram. This is more than enough to accomplish the task I have in mind.··I recently purchased the new Propstik kit, if I read the specs correctly, it will give me
the following:
Instead of purchasing a BS2P,
I guess it's time to go back to kindergarten and learn how to write spin programs.·· Thanks again, the help I get in this forum
it exceeds all expectations
Larry