Use String to reference Variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Use String to reference Variable

Pedro Pessoa
Hello!
I want a function that takes a string as arg an from that produces a valid
variable reference, as follows:

%%% pseudo
Nabc={a1 d e f}
Nxyz={b1 e a d}

fun=
#(define-music-function (x)(string?)
   #{
     <<
       #(concat x "abc")
       \\
       #(concat x "xyz")
     >>
   #})

\fun "N" %produces parallel music with Nabc and Nxyz
%%%

---

I've ran this test:

%%%
\Nabc={some music}
(display (string->symbol (string-append "N" "abc")))
%%%

It outputs "Nabc", not the music content of Nabc, as I expected.
Why is that? How do I make it point to the actual music?




--
Sent from: http://lilypond.1069038.n5.nabble.com/User-f3.html

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: Use String to reference Variable

Jan-Peter Voigt
Hello Pedro,

Am 04.12.18 um 04:28 schrieb Pedro Pessoa:

> Hello!
> I want a function that takes a string as arg an from that produces a valid
> variable reference, as follows:
>
> %%% pseudo
> Nabc={a1 d e f}
> Nxyz={b1 e a d}
>
> fun=
> #(define-music-function (x)(string?)
>    #{
>      <<
>        #(concat x "abc")
>        \\
>        #(concat x "xyz")
>      >>
>    #})
>
> \fun "N" %produces parallel music with Nabc and Nxyz
> %%%
>
> ---
>
> I've ran this test:
>
> %%%
> \Nabc={some music}
> (display (string->symbol (string-append "N" "abc")))
> %%%
>
> It outputs "Nabc", not the music content of Nabc, as I expected.
> Why is that? How do I make it point to the actual music?
The string is converted to a symbol and a symbol is a primitive datatype
in guile-scheme. To receive the value of the variable you have to ask
the parser. To place the result in the music you should use an instant
scheme expression (introduced by '$' not '#').

HTH:

fun=
#(define-music-function (x)(string?)
   #{
     <<
       $(ly:parser-lookup (string->symbol (string-append x "abc")))
       \\
       $(ly:parser-lookup (string->symbol (string-append x "xyz")))
     >>
   #})


Jan-Peter

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: Use String to reference Variable

David Kastrup
Jan-Peter Voigt <[hidden email]> writes:

> Hello Pedro,
>
> Am 04.12.18 um 04:28 schrieb Pedro Pessoa:
>> Hello!
>> I want a function that takes a string as arg an from that produces a valid
>> variable reference, as follows:
>>
>> %%% pseudo
>> Nabc={a1 d e f}
>> Nxyz={b1 e a d}
>>
>> fun=
>> #(define-music-function (x)(string?)
>>    #{
>>      <<
>>        #(concat x "abc")
>>        \\
>>        #(concat x "xyz")
>>      >>
>>    #})
>>
>> \fun "N" %produces parallel music with Nabc and Nxyz
>> %%%
>>
>> ---
>>
>> I've ran this test:
>>
>> %%%
>> \Nabc={some music}
>> (display (string->symbol (string-append "N" "abc")))
>> %%%
>>
>> It outputs "Nabc", not the music content of Nabc, as I expected.
>> Why is that? How do I make it point to the actual music?
> The string is converted to a symbol and a symbol is a primitive datatype
> in guile-scheme. To receive the value of the variable you have to ask
> the parser. To place the result in the music you should use an instant
> scheme expression (introduced by '$' not '#').
>
> HTH:
>
> fun=
> #(define-music-function (x)(string?)
>    #{
>      <<
>        $(ly:parser-lookup (string->symbol (string-append x "abc")))
>        \\
>        $(ly:parser-lookup (string->symbol (string-append x "xyz")))
>      >>
>    #})

In this particular case, # would have worked though $ tends to work in
more cases.  However, $ also creates a _copy_ of the music while #
doesn't.  If the music ends up in a \relative or \transpose or other
construct modifying its content in place, having a copy is important so
that the original variable does not get changed.

\xabc creates a copy like $xabc does, while #xabc doesn't.

--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user