Accessing a Variable through User Input

I have a list of variables containing lists of musical notes:

 Smoke on the Water
 Frere Jacques

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?

That is how my Queries work in this project:

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!

that is a cool project!

I'm now thinking of how images in my MYO scanimation project (remixed from Jens's project) creates a new variable for each list of frames...

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.

Thanks for the follow-up. To clarify what I would like to do -

If the user enters “Smoke on the Water” in response to “Ask”:


I can now tell that a variable named “Smoke on the Water” exists.


I would like to use the user input in response to ASK to access the MIDI notes associated with “Smoke on the Water” and play the notes in the list:


Wow, you like flat design? And you're a grownup!

I understand what you want. I'm suggesting you achieve it this way: For each song, you do this:

Then you can do this:

(ASSOC is in the List Utilities library.)

Great .... Thanks for the illustration!

The closest to a list of variables I think would be a list of ringified variables.

Good point, you could get it to work that way too.

"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.