Le 18/10/2020 à 21:42, Matthew Fong a écrit :
Hello everyone,
I am having a bit of an issue storing a variable in an alist, and I
was wondering if you all could point out what I am doing wrong. If I
write the string directly, there's no issue retrieving it ...
my-alist = #'((1 . "A") (2 . "B") (3 . "C"))
value = #(ly:assoc-get 2 my-alist " error")
\markup \value
hymnID = "210"
hymnMeter = "8.8.8.8"
hymnAlist = #'(("ID" . \hymnID ) ("Meter" . \hymnMeter ))
hymnValue = #(ly:assoc-get "ID" hymnAlist "error")
\markup \hymnValue
Many thanks,
mattfong
Hello,
Take a look at the output of:
hymnID = "210"
hymnMeter = "8.8.8.8"
hymnAlist = #'(("ID" . \hymnID ) ("Meter" . \hymnMeter ))
#(display (ly:assoc-get "ID" hymnAlist "error"))
This prints
\hymnID
The reason is that you created the alist using quoting, and a piece of
text that you quote is a symbol. Note that prefixing something with a
backslash is not the way to use a LilyPond variable from Scheme; you
just need the naked name. Here, the values (as opposed to keys) in
hymnAlist are symbolic values representing the identifiers “\hymnID” and
“\hymnMeter” (backslashes are valid in Scheme identifiers!).
You can read more about quoting here:
https://scheme-book.ursliska.de/scheme/quoting/README.html
The correct way to go about it is to use quasi-quoting:
hymnID = "210"
hymnMeter = "8.8.8.8"
hymnAlist = #`(("ID" . ,hymnID ) ("Meter" . ,hymnMeter ))
\markup #(ly:assoc-get "ID" hymnAlist)
(note ` instead of ' and the commas).
Alternatively,
hymnAlist = #(list (cons "ID" hymnID ) (cons "Meter" hymnMeter ))
has the same effect.
Best,
Jean