Shop OBEX P1 Docs P2 Docs Learn Events
combining variables (cancatenation) — Parallax Forums

combining variables (cancatenation)

lfreezelfreeze Posts: 174
edited 2007-09-27 22:31 in Propeller 1
Using spin is it possible to combine variables as follows:

···· variable1:=5
···· variable2:=6

I want to combine these into a result that· will equal···· variable3:=56······· I've done searches on the forum and don't see
this question. I have tried the manual and find no reference to it.
Thank you in advance for any responses

Larry

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-09-27 17:45
    It's simple arithmetic: variable3 := variable1 * 10 + variable2.

    Perhaps you're trying to do something more complicated?
    You'll get a more useful answer if you explain more about what you want to do.
  • lfreezelfreeze Posts: 174
    edited 2007-09-27 17:54
    Thanks for quick response.· Please excuse the way I posed the question. I am trying to cancatenate variables.·· I guess it would be easier to explain this way.....

    variableA:=A

    variableB:=B

    I am trying to get a new variable from variableA and variableB that will equal···· variableC:=AB

    Larry
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-09-27 18:03
    When you say variable do you mean a number or a string?

    It's concatenate by the way.

    Graham
  • SapiehaSapieha Posts: 2,964
    edited 2007-09-27 18:04
    Hi Ifreze.

    Combine a+b with shift a 8 bits to high order in word of variable a

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nothing is impossible, there are only different degrees of difficulty.

    Sapieha
  • lfreezelfreeze Posts: 174
    edited 2007-09-27 19:01
    Sorry folks I started a new thread in error on this subject, I will continue here

    I ·want to use numbers, I used letters in the example previous example to make the question easier to understand.· here are a few examples of· what I am trying to accomplish. there is no math function involved just putting the orginal variables side by side in a new variable

    ···· variable1:=1
    · ·· variable2:=1
    I want variable3 to equal· ·11

    ·· variable1:=4
    ·· variable2:=5
    I want variable3 to equal 45

    ·· variable1:=8
    ·· variable2:=7
    I want variable3 to equal 87

    Thanks
    Larry
  • SapiehaSapieha Posts: 2,964
    edited 2007-09-27 19:15
    Hi Ifreeze.

    This is typical BCD variable.
    Combine it in 2 Nibles

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nothing is impossible, there are only different degrees of difficulty.

    Sapieha
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-27 19:33
    Larry, you are frustrating a lot of persons here. Can yue please put your question in a way that makes sense?

    You have got a perfectly valid answer from Mike, bt you are still repeating your - most like incomplete and misunerstandable - examples.

    I have the shrude idea that you are after some string concatenation, but this is not at all obvious.
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-09-27 19:35
    DeSilva said...
    I have the shrude idea that you are after some string concatenation, but this is not at all obvious.


    DeSilva - I am pretty sure that is what he means ..

    Regards,
    John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'
  • lfreezelfreeze Posts: 174
    edited 2007-09-27 20:02
    I am sorry for the confusion.· Yes, I am after concatenation of numbers. I am not sure how to express this

    question other than by providing examples of what I am looking ·for.· If· ·I concatenate the number 1, and

    the number 2, I get 12.·· I was trying to show this in my previous examples, obviously I failed.

    Thank you all again I hope this adds some clarity to my question.

    Larry
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-09-27 20:08
    Well if that is really what you want to do Mike answered the question in his first post:

    variable3 := variable1 * 10 + variable2.

    if variable1 = 1 and variable2= 2 then variable3 = 1*10+2 = 12

    Graham
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-27 20:10
    I think that you need to understand the difference between the text representation of a number and the number itself. Is it the characters representing the numbers you want to concatenate? Or is it simply a question of adding units and tens together?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-27 20:21
    There's more than one way to do what you're after, and which one you choose depends on your objectives which, so far, are shrouded in mystery. The v1*10+v2 method makes it possible to see your "concatenation" when displayed as a decimal number. Another representation is BCD, which would be obtained via (v1<<4)+v2. With this method you can pack up to 8 digits in a long and be able to see them when the long is displayed as a hex number. A third method involves ASCII strings: ((v2+"0")<<8)+v1+"0". With this method up to three digits can be packed into a long and displayed as a string. Strings require a terminating 0 byte, so three is all you get without going to a byte array.

    So tell us, please, what is your final objective here?

    -Phil
  • SapiehaSapieha Posts: 2,964
    edited 2007-09-27 20:36
    Hi Ifreeze.

    Problem is only att.

    1 = 0001 - Binary in Nible BCD
    1= 00000001 - in Byte binary
    and
    "1" = 00110001 in ASCI(string) in byte binary.

    finaly you
    variable1:=4
    variable2:=5
    I want variable3 to equal 45

    4 = in ASCI - 0011_0100
    5 = in ASCI - 0011_0101
    If you mask high Nible on it
    4 = 0100
    5 = 0101
    variable3 to equal 45 = in Byte 0100_0101
    In Nibles
    (0100 <<4) + 0101 = Byte 0100_0101

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nothing is impossible, there are only different degrees of difficulty.

    Sapieha
  • BasilBasil Posts: 380
    edited 2007-09-27 20:56
    Hi Larry,

    Var 1 = Byte = 4
    Var 2 = Byte = 5

    'Var3 := (Var1 << 8) & Var2' works for me [noparse]:)[/noparse]

    This wouldn't give you '45' as a number, but would give you a '4' beside a '5' in the same word/long. Is that what you mean?

    Same as what Sapieha is saying I believe?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Alec

    My our page
  • lfreezelfreeze Posts: 174
    edited 2007-09-27 21:17
    Phil,

    Thank you for taking the time to reply. I am becoming very frustrated with my inability to express my questions correctly I didn't add my final objective to the previous posts as I didn't want to add any additional confusion. My final objective with this project is to receive·single serial digits from a keyboard·into the propeller. I am trying to combine (concatenate) these random digits into groups of two digits as a single variable as soon as they are received by the propeller.

    ·I don't want to do any math with them, simply concatenate them, and then to export them to a BS2.· I am looking for a simple way to do this. I now think, that is not possible. I was hoping I could avoid writing a· large (if then) piece of code. such as

    if variable1 = 1 and variable2 =2·· then variable 3 = 12

    ·if variable1 = 1 and variable2 =3·· then variable 3 = 13

    if variable1 = 5 and variable2 =4·· then variable 3 = 54

    ETC. ETC.

    My original plan is to use incoming digits,· and pass them back out to a bs2 which will be set up to· take specific actions based on the variables. It is a robotic application with each number providing some form of· control.··

    Larry
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-27 21:25
    Larry,

    That helps a lot, since it tells me you want two digits "packed" (perhaps a better word than "concatenated") into a byte. That's easy using the BCD method:

    pack := (v1 << 4) + v2

    mentioned above. The BS2 would then unpack them as follows:

    v1 = pack >> 4 : v2 = pack & $0F

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-27 21:44
    @lfreeze: Please notice that my esteemed colleagues have not been very precise wrt "digit", or "byte" during their last postings wink.gif . You have always to take into account that "0" is %0011_0000 rather than %0000_0000
  • Mark BramwellMark Bramwell Posts: 56
    edited 2007-09-27 21:49
    Your right deSilva. I am assuming he has everything in a numeric (non-string) variable such as a long, otherwise the simple math functions don't work.

    --- original posting ---

    If the digits are coming in 1 at a time, do something like so:

    set Total = 0
    set NewNum = 0

    Assume "Total" = what you have so far
    Assume "NewNum" is the new digit that is arriving

    Each time a new number arrives,

    Total = (Total * 10) + NewNum

    For example:
    If total was zero, and 1 arrived, we would have (0 * 0) + 1 = 1
    If total was 1, and 2 arrived, we would have (1 * 10) + 2 = 12
    If total was currently 12 and 7 arrived as a new digit, the math would make it 12 * 10 giving us 120. We then add the new number giving us "Total = 127"

    Does the above appear to fit your desired input/output?


    Simple, fast and no strings or bit flipping involved.
  • BasilBasil Posts: 380
    edited 2007-09-27 21:51
    Hi Larry,

    That is very similar to what I have done in my thing.

    Using 'Var3 := (Var1 << 8) & Var2' I combined Var1 with Var 2 into Var3 which I then sent as a single variable into a function.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Alec

    My our page
  • SapiehaSapieha Posts: 2,964
    edited 2007-09-27 21:55
    Hi Basil.

    You method works BUT.
    You not have spare memory.
    You only fill Word with 2 Byte
    In Nible method 2 Byte numbers have one byte place.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nothing is impossible, there are only different degrees of difficulty.

    Sapieha
  • BasilBasil Posts: 380
    edited 2007-09-27 21:55
    Good point [noparse]:D[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Alec

    My our page
  • mirrormirror Posts: 322
    edited 2007-09-27 22:13
    Larry,

    If:
    VarA := 123
    VarB := 567

    then
    VarC := ????

    Only you can answer this question, and your answer will determine the level of help that we can give to you.

    if VarC == 123567, then a possible solution to your problem is:

    VarC := Pow10(1+Int(Log10(VarB))*VarA + VarB

    where:

    Pow10(v) := v to the power of 10
    Log10(v) := the base 10 logarithm of V
    Int(v) := the integer part of v i.e. drop the fraction.

    That's a horribly complicated solution though!

    I'd probably write the solution·as:
    ·
    Assuming VarB < 10_000
     
    if VarB >= 1000
      VarC := VarA * 10000
    else if VarB >= 100   VarC := VarA * 1000
    else if VarB >= 10   VarC := VarA * 100
    else   VarC := VarA * 10
    VarC += VarB
    
    
    
     
    

    ·

    Post Edited (mirror) : 9/27/2007 10:21:11 PM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-09-27 22:29
    I think you will find that Phil's post addresses what he actually wants to do. he wants to take two numbers with only 4 significant bits and put them into a single 8 bit number. Basically binary coded decimal.
  • lfreezelfreeze Posts: 174
    edited 2007-09-27 22:31
    Thanks everyone.... Based on your responses, I have a lot of solutions to try. I believe Marks· more closely fits my requirement I will try his first. I am always amazed that on this forum beginners like me ask confusing and obscure questions, yet each time the answers from the experts are always·quick and extensive.....

    Larry

    .
Sign In or Register to comment.