Shop OBEX P1 Docs P2 Docs Learn Events
Word array into a long array — Parallax Forums

Word array into a long array

Dave MatthewsDave Matthews Posts: 93
edited 2014-04-09 11:29 in Propeller 1
I would like to take a word size array and feed it into a long size array.
I would like the wordtest[0] element to become the longtest[0] element ... wordtest[1] to become longtest[1],... and so on.

Dave

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-08 08:43
    This may not be the slickest or trickiest, but it is blazingly obvious (which I think code should be in most cases).
    pub move(p_la, p_wa, count)
    
    '' Move values in word array to low words of long array
    '' -- p_la is pointer to long array (dest)
    '' -- p_wa is pointer to word array (src)
    '' -- count is number of elements to move (i.e., copy)
    
      repeat count
        [B]word[/B][p_la] := word[p_wa]                                    ' copy element
        p_la += 4                                                   ' update long pointer
        p_wa += 2                                                   ' update word pointer
    


    This assumes that you do not want the upper word of the long array to be modified by the copy. If you don't want to keep the original high word contents in the long array, you can do this with a simple loop, or modify the above code like this
    pub move(p_la, p_wa, count)
    
      repeat count
        [B]long[/B][p_la] := word[p_wa]                                    ' copy element
        p_la += 4                                                   ' update long pointer
        p_wa += 2                                                   ' update word pointer
    


    The reason for pointers is that the method will work with any arrays you have.
  • Dave MatthewsDave Matthews Posts: 93
    edited 2014-04-08 12:24
    Thank you Jon, once again!

    I do want the upper words to be zeroed so the simple loop is for me (didn't see the other loop?).
    I am grateful that you did not have to refer me to a spin language command that would do the same thing, I always try to check everything and OBEX before posting a question here.
    At any rate your description is very clear and I am very grateful.

    Dave
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-08 16:44
    If you know the names of the arrays it's as simple as:
    repeat idx from 0 to len-1
        longarray[idx] := wordarray[idx]
    


    Boom! Done. Now, this is fine if you only have to two arrays. If there are more then you can use the second snippet above and pass pointers.
    move(@longarray, @wordarray, 10)
    


    ... for example, if you had 10 elements to move. BTW... names are resolved to pointers internally, so getting comfortable with them is a useful way to create more flexible code.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-08 18:33
    I was having an offline exchange with another forum regular (and far more clever guy than me) -- he pointed out another solution using pointers.
    pub move(p_la, p_wa, count) : n
    
      repeat count
        long[p_la][n++] := word[p_wa][n]
    


    This uses a local variable as an index. I know it seems odd to have the post-increment on the left side but this is where it belongs; we don't want to increment N until the value has been moved from one array to another. Note, too, that he's actually using the result variable (this is noted by the use of : after the method name); this variable is always set to zero on entering a method.
  • Dave MatthewsDave Matthews Posts: 93
    edited 2014-04-09 11:29
    Excellent, thanks!

    Dave
Sign In or Register to comment.