In this question, I learned how to cascade or nest interdependent Setter
and SetterBar
controls with numerical Ranges. The puzzle has arisen again, this time with lists containing symbols. This puzzle is not, to me, in any obvious way a duplicate of the prior one.
Consider this miniature database of destination cities:
destinations = {SLC, DEN, OKC, MSY, ABQ};
and this function (as a lookup table) from destination city to pair of nearby source cities:
src[SLC] = {ONT, PHX};
src[DEN] = {PHX, DFW};
src[OKC] = {DFW, SAT};
src[MSY] = {DFW, TPA};
src[ABQ] = {DFW, PHX};
My first attempt at nested controls, allowing one to choose a destination city and then choose one of the nearby source cities, produced a surprise:
Manipulate[
{dest, source},
{dest, destinations, SetterBar},
{{source, src[dest][[1]]}, src[dest], SetterBar}]
Notice that, while the pair of sources for each destination are correctly pulled from the src
function, there is no default selected item in the lower SetterBar even though I explicitly set it to the first item src[dest][[1]]
. The default source
seems always to be (inexplicably) the first city in destinations
list. The source
SetterBar works correctly when I click in it, but its default is never set correctly.
I can work around the problem by mapping through indices:
Manipulate[
{dest, src[dest][[source]]},
{dest, destinations, SetterBar},
{{source, 1}, {1 -> src[dest][[1]], 2 -> src[dest][[2]]}, SetterBar}]
but this is not a robust solution because it requires me to hardcode the list of button labels in a set of rules. What if I wanted three sources for one of the destinations?
MapIndexed[#2[[1]] -> # &, src[dest]]
in place of{1 -> src[dest][[1]], 2 -> src[dest][[2]]}
to cover cases wheresrc[_]
can be of any length? $\endgroup$src
anAssociation[]
? $\endgroup$Dynamic
). $\endgroup${{source, src[dest][[1]]}, src[dest], SetterBar}
, associations produce the same undesirable results as my first proposal. I have long hypothesized that associations are just an alternative syntax for the traditional, single-bracket, pseudo-functional, pattern-matching notation likesrc[dest]
, which produces an internal lookup table (almost certainly a hash table). Semantically, I don't know of a difference between an association and the equivalent set of single-bracket definitions like my original definition ofsrc
. $\endgroup$