Re: [Csnd-dev] [EXTERNAL] Re: [Csnd-dev] convert string to variable name

Victor Lazzarini <[email protected]>
Newsgroups gmane.comp.audio.csound.devel
Message-ID <[email protected]>
it seems serendipitous to talk about ref types since Steven put a comment in this closed issue:

https://github.com/csound/csound/issues/1661#issuecomment-3169348066

which seems to be in the direction I'd like to see. However someone will need to go and implement it.

We could open an issue on this but OTOH I hate to see issues opened that are never dealt with and ten years later they're still there.

So I don't really know what to do.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 10 Aug 2025, at 14:28, Eduardo Moguillansky <[email protected]> wrote:


*Warning*

This email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.

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]<mailto:[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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.