Re: [Csnd-dev] convert string to variable name
Eduardo Moguillansky <[email protected]>
| Newsgroups | gmane.comp.audio.csound.devel |
|---|---|
| Message-ID | <CAHFFssKygUrFJRpfaXakAVSDHpqiwJRCt=+vqix+RbbBv3jrKw@mail.gmail.com> |
evalstr works at the instr0 level and can only return scalar values.
Using ref/deref it can be done (see example below), but needs an external
plugin. Maybe we could add these opcodes to csound itself?
instr 1
arr_1:i[] = [1,2,3]
arr_2:i[] = [4,5,6,7]
iregistry = dict_new("str:float", "arr1", ref(arr_1), "arr2", ref(arr_2))
if eventtime() > 0.05 then
; modifying the original array modifies the reference
arr_1[0] = 10
endif
; switch between the two at each cycle
Skey = sprintfk("arr%d", eventcycles() % 2 + 1)
karr[] = deref(dict_get:k(iregistry, Skey))
println "Skey: %s", Skey
printarray karr
endin
schedule 1, 0, 0.1
On Sun, Aug 10, 2025 at 11:20 AM joachim heintz <[email protected]> wrote:
> is it possible to convert a string to a variable name in csound 7?
> use case:
>
> // having two string variable names
> arr_1:i[] = [1,2,3]
> arr_2:i[] = [4,5,6]
> // s will be either "arr_1" or "arr_2"
> s:S = sprintf("arr_%d",int(random:i(1,2.9999)))
> // the (not existing) opcode str2var would convert to the variable name
> myvar = str2var(s)
> // so now i can use myvar
> print(myvar[0])
>
> or something like python's eval() in csound?
>
> (i tried the existing opcode evalstr but could not get to what i need.)
>
> thanks -
> joachim
>