I have a list of variables containing lists of musical notes:
Smoke on the Water
Frere Jacques
Etc.
I would like to use the ASK command to allow the user to enter the name of a tune to play. Is there a way to access the variable associated with text entered by the user?
yeah, you could use that JS kludge that everybody on this forum can't do without. Or you could just not associate your songs with individual named variables, but put them into a data base - a list - instead. Then you would use the FIND block to look up the song associated to the user input. And if the result of FIND happens to be false that means there is no such entry in your data base.
The data base would be a list of two-item lists, each row consisting of a name and the musical data. Make sense?
Seeing this suggestion here reminds me of everything I hate about these libraries. They encourage the absolute worst practices for simple problems imaginable. Seriously, we should get rid of most of the meta and Eisenberg stuff, because kids feel that in order to be smart they have to use obscure features. Kids, the cool stuff is being able to think straight and write a simple short script rather than use JS!
No, you don't; variables are not first class. You can have a list of names of variables, or you can have a list of values of variables. But imho what you want is sort of both: a list in which names are associated with values -- but once you have them both in the list, there's no need for variables at all. What you have is an association list, and you find an item in that list by searching through the first items of each item, and then use the second item of the one that matched.
"assoc" is a name given to this kind of function over 50 years ago. But shouldn't we encourage better names in Snap? E.g. "Find association of ... in ..." or ...
I spoze. I was writing a whole library of Lisp functions so I just used the Lisp names without thinking too hard. :~) If we rewrite that, I wouldn't even use "association"; what non-Lispians call an alist is a dictionary. So "look up _ in dictionary _" or something like that.
Edit: But anyway, now that we have FIND FIRST we can efficiently write assoc as a trivial one-liner. I didn't do it that way in the library because KEEP traverses the entire alist even if it finds a match right away.