Csound Csound-dev Csound-tekno Search About

[Csnd-dev] convert string to variable name

Date2025-08-10 10:20
Fromjoachim heintz
Subject[Csnd-dev] convert string to variable name
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

Date2025-08-10 18:28
FromEduardo Moguillansky
SubjectRe: [Csnd-dev] convert string to variable name
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 <jh@joachimheintz.de> 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

Date2025-08-11 02:08
Fromvlz
SubjectRe: [Csnd-dev] convert string to variable name
It’s possible to create a new variable but you need to recompile the code.
> On 10 Aug 2025, at 10:20, joachim heintz  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

Date2025-08-11 22:50
FromVictor Lazzarini <000010b17ddd988e-dmarc-request@LISTSERV.HEANET.IE>
SubjectRe: [Csnd-dev] [EXTERNAL] Re: [Csnd-dev] convert string to variable name
it seems serendipitous to talk about ref types since Steven put a comment in this closed issue: 


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 <eduardo.moguillansky@gmail.com> 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 <jh@joachimheintz.de> 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

Date2025-08-12 12:44
Fromjoachim heintz
SubjectRe: [Csnd-dev] [EXTERNAL] Re: [Csnd-dev] convert string to variable name
yes perhaps it is something for later --- i think we should concentrate 
now on releasing csound 7 without somehow luxurious wishes like that ...


On 11/08/2025 23:50, Victor Lazzarini wrote:
> 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 
>>  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 > > 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
>>