MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to scope PopupMenu values in a DynamicModule?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107458] Re: How to scope PopupMenu values in a DynamicModule?
  • From: asdf qwerty <bradc355113 at yahoo.com>
  • Date: Sat, 13 Feb 2010 05:22:46 -0500 (EST)
  • References: <hl0r15$tq$1@smc.vnet.net> <hl37q2$edv$1@smc.vnet.net>

I actually am using labels in my code; I was just trying to keep the
example simple. The code readability problem isn't here in the
PopupMenu, it's elsewhere, where I process the value of the variable
by switching on its value. I don't want to have to have a bunch of
cases labeled by cryptic integers. I'd like to do something like this
(which doesn't work):

DynamicModule[
    {x, even, odd},
    {
        PopupMenu[Dynamic[x], {
            even -> "even",
            odd -> "odd"
        }],
        (* ...intervening code... *)
        Dynamic[Switch[x,
            even, 2 n,
            odd, 3 n + 1
        ]]
    }
]

In the meantime, I noticed that if I assign integer values to the
symbols, then everything works, and it seems to still scope my symbols
safely. I.e., I can't break the running Dynamics by assigning new
values to the mangled names (which contain dollar signs) like I can if
I just use a regular Module. The working code looks like this:

DynamicModule[
    {x, even = 0, odd = 1},
    {
        PopupMenu[Dynamic[x], {
            even -> "even",
            odd -> "odd"
        }],
        (* ...intervening code... *)
        Dynamic[Switch[x,
            even, 2 n,
            odd, 3 n + 1
        ]]
    }
]

I still don't really understand why this change makes it work when it
didn't work before though...

On Feb 12, 1:42 am, "Sjoerd C. de Vries" <sjoerd.c.devr... at gmail.com>
wrote:
> If you want to have labels in the pop-up for better readability why
> don't you use the
>
> PopupMenu[x,{Subscript[val, 1]->Subscript[lbl, 1],Subscript[val, 2]-
>
> >Subscript[lbl, 2],\[Ellipsis]}]
>
> syntax version of PopupMenu?
>
> Cheers -- Sjoerd
>
> On Feb 11, 1:52 pm, asdf qwerty <bradc355... at yahoo.com> wrote:
>
> > Why does this work:
>
> > DynamicModule[{foo}, PopupMenu[Dynamic[foo], {bar, baz}]]
>
> > but this doesn't:
>
> > DynamicModule[{foo, bar, baz}, PopupMenu[Dynamic[foo], {bar, baz}]]
>
> > ?
>
> > In the second example, the PopupMenu display always looks blank. Are
> > the two occurrences of bar and baz in different scopes or something?
>
> > I ran into this because I'm trying to avoid possible symbol clashes
> > for my PopupMenu values, for the reason described near the beginning
> > of the "Advanced Dynamic Functionality" tutorial ("a different kernel
> > session may coincidentally share the same localized variable names").
> > I'd like to use symbols for the PopupMenu values rather than say,
> > integers, because it makes the code easier to read.


  • Prev by Date: Re: Random number with custom distribution
  • Next by Date: Re: Re: Define an antisymmetric function
  • Previous by thread: Re: How to scope PopupMenu values in a DynamicModule?
  • Next by thread: Define an antisymmetric function