Shop OBEX P1 Docs P2 Docs Learn Events
cancatenation of variables — Parallax Forums

cancatenation of variables

lfreezelfreeze Posts: 174
edited 2006-07-28 20:44 in BASIC Stamp
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
·

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-26 13:47
    Larry -

    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 -->
  • lfreezelfreeze Posts: 174
    edited 2006-07-27 00:40
    Bruce,
    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
  • HarborHarbor Posts: 73
    edited 2006-07-27 05:24
    lfreeze said...
    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
    Concatenation is a term for string manipulation, Larry, and it is not an in-built function of this version of Basic. Microprocessors like the Stamp family do not have enough variable space to manipulate strings of any signficant length, so there wouldn't be any point in having the operators for that in the language.

    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
  • lfreezelfreeze Posts: 174
    edited 2006-07-27 18:55
    Thank you very much for the detailed response. After looking at the options, I'll take the suggestion in your last paragraph. I am also developing E-mail messages extracted from stamp data logged over each 24 hour period. In my effort to compile condensed variables, I lost sight of the limitations of variable space the stamp offers.

    Larry
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-27 19:29
    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 -->
  • lfreezelfreeze Posts: 174
    edited 2006-07-28 11:58
    Bruce,

    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:

    attachment.php?attachmentid=74021

    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
    416 x 136 - 3K
  • HarborHarbor Posts: 73
    edited 2006-07-28 20:44
    I think it's 127 bytes for the BS2P, but it doesn't change your point. Learning a new processor and support tools is always worth the effort in the long run. Glad we could help, Larry.
Sign In or Register to comment.