I have a list of treatments:


I want to use this list to populate a PopupMenu. The number of treatments can change at runtime. If I choose the first treatment I want to get a value 1, if second then 2 and so on. It does not matter what their names are.

How do I implement this?


1 Answer 1

max = 5;
choices = RandomWord["Verb", max]
wts = Range[max]
pdata = AssociationThread[choices, wts]
<|"obtrude" -> 1, "enrich" -> 2, "seam" -> 3, "chap" -> 4, 
 "hurdle" -> 5|>
x = RandomChoice[Keys@pdata]
  Dynamic[x], Keys@pdata]
 , Dynamic[pdata[x]]

If you are already using a DynamicModule then:

DynamicModule[{x0 = RandomChoice[Keys@pdata]},
 {PopupMenu[Dynamic[x0], Keys@pdata]
  , Dynamic[pdata[x0]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.