Possible Duplicate:
How to extract the numerical value of a dynamical variable
Dynamic wraps a list that I cannot extract values from

I want to extract value from list dynamically,this is my code

xData = {"Environment", "Building"};
 Row[{tabP1 = 
    Dynamic[Position[xData, ToString[buttonValue]], 
     t = tabP1[[1, 1]]]}]]
Button[button1, buttonValue = Environment] Button[button2, 
  buttonValue = Building]

It gives {{1}} or {{2}} based on button i have clicked. Now i want to extract value(1) from this list({{1}}) . I have tried to extract it as follows t=tabP1[[1,1]] but t has list({{1}}) not value(1). I need value instead of list.

  • 1
    $\begingroup$ @Mr.Wizard Same topic, but in this case I could see what the OP's problem is. $\endgroup$ – Michael E2 Jan 12 '13 at 14:10
  • 2
    $\begingroup$ @Michael that's a good point; maybe both should be closed as a duplicate of mathematica.stackexchange.com/q/15090/121 ? $\endgroup$ – Mr.Wizard Jan 12 '13 at 14:40
  • $\begingroup$ @Mr.Wizard Fair enough. That answer works here, although the OPs' misconceptions about Dynamic seem different. $\endgroup$ – Michael E2 Jan 12 '13 at 16:06

I'm a little unclear about the exact functionality you seek. Below is an example that has the desired effect you describe. It may not be exactly how one would do it in the context of a larger project.

You need to have the assignments inside (the first argument of) the Dynamic. The second argument should be a function and is used primarily in interactive controls (Slider, InputField, etc., but not Row). When the variables in the first Dynamic expression change (buttonValue), the expression is reevaluated, making the assignments happen.

xData = {"Environment", "Building"};
buttonValue = "Environment";
 Row[{Dynamic[tabP1 = Position[xData, buttonValue]; t = tabP1[[1, 1]]; tabP1]]}]]
 Button["button1", buttonValue = "Environment"] Button["button2", 
  buttonValue = "Building"]

I made a couple of other changes (initialized buttonValue, changed names to strings -- consider them suggestions), but left some other oddities alone. In particular, the Buttons are multiplied. I would guess you want to use Row, or you're just testing and didn't care that much.

Other suggestions go beyond the question: You can put the Dynamic around the whole Row instead of the first element, but that may depend on what else you might add to the row. You can put it all in one Manipulate. The assignment to tabP1 and t can be done via each button's action:

updateTab[key_] := (tabP1 = Position[xData, key]; t = tabP1[[1, 1]]);
Button["button1", updateTab["Environment"]] Button["button2", 

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