Cabbage Logo
Back to Cabbage Site

Using strcpyk and a temp string variable to swap 2 items in an array

The following code is intended to swap the first and second items in the array Smodules[] every time a button (“right1”) is pressed and print the second item to the terminal.

Currently it prints to the terminal each time the button is pressed, but as far as I can tell it only swaps the array items on initialization and never when the button is pressed. Given my understanding of strings, arrays, and the strcpyk opcode in Csound, I have no explanation for this.

Smodules[] fillarray "pitchmodule", "formantmodule", "partialsmodule", "smearmodule" 

kArrowsChanged = changed(cabbageGetValue("right1"))

if kArrowsChanged == 1 then
    if cabbageGetValue("right1") == 1 then
        Stemp strcpyk Smodules[0]
        Smodules[0] strcpyk Smodules[1]
        Smodules[1] strcpyk Stemp
        printks Smodules[1], 0
    endif
endif

I’m stumped. I just posted a question on the Csound discord server to ask there. :wink:

I came up with a botched (albeit functional) solution using an array of numbers and strset.

kmodules[] fillarray 0, 1, 2, 3 
ktemp = 0

strset 0, "pitchmodule"
strset 1, "formantmodule"
strset 2, "partialsmodule"
strset 3, "smearmodule"

kArrowsChanged = changed(cabbageGetValue("right1"))

if kArrowsChanged == 1 then
    if cabbageGetValue("right1") == 1 then
        ktemp = kmodules[0]
        kmodules[0] = kmodules[1]
        kmodules[1] = ktemp
   endif
endif

Whenever I want to use a string out of the array, I use strget(kmodules[index]). The only annoying thing about this solution is that strget only runs at i-rate, so I have to put anything that uses it more often into a separate instrument.

Hope this helps anyone else who has a similar problem.

Thanks for the solution. It’s frustrating that the original code doesn’t work. I’d love to know why.