5

Within the debugger, can we watch local variables within a Module?

As the screenshot shows, the “i” only display its global value (12) upon a message breakpoint. Is there a way to show its local value? enter image description here

Clear[i];

i = 12; f[] := Module[{i}, For[i = 1, i < 10, i++, Print[i]; Print[":\t"]; Print[i/0]]; ];

f[]

Jimmy

JimmyLin
  • 573
  • 2
  • 11
  • 2
    Hummm... I would say that Mathematica debugger is not a good option for debug. I hope that the new Wolfram Language could came with a new Wolfram Debugger. :p – Murta Jan 04 '14 at 20:14
  • 1
    It is not showing the local value because it is not really i within the module - it's some temporary variable. – VF1 Jan 04 '14 at 20:17
  • @vf1: Do you know how to get the actual name of the temporary variable so we can watch them, like in Visual studio? – JimmyLin Jan 04 '14 at 20:18
  • @Murta: link, user Szabolcs metioned that the debugger is "immensely helpful" because it can watch local variable. Any idea on how he achieved that? – JimmyLin Jan 04 '14 at 20:19
  • @Murta: the previous link was wrong, please see this – JimmyLin Jan 04 '14 at 20:22

1 Answers1

3

Edit

I figured it out - never used the debugger in MMA before so that's why I improvised. The Stack window has a "Local variables" tab - just open that: enter image description here

Old answer

You can get the actual temporary name of the variable by printing it before it has a value. I'll see if there's a nicer solution.

enter image description here

VF1
  • 4,702
  • 23
  • 31
  • This is however not sufficient. For example, what if "i" is the index of an array and we want to display value of myArray[i]? The small expandable "Local variable" icon shows only the value, but that value cannot be used. – JimmyLin Jan 04 '14 at 20:39
  • @JimmyLin your question states "Is there a way to show its local value?" - for the time being, you can copy and paste. If you're looking for a Visual Studio level of debugging, I recommend Wolfram Workbench. – VF1 Jan 04 '14 at 20:43
  • @JimmyLin even copy/paste is unnecessary - you can directly edit the local variables cell. – VF1 Jan 04 '14 at 20:46
  • If you change "Module" to "Block", could you notice any difference? On my machine now I can access (both view and edit) the local variable. – JimmyLin Jan 04 '14 at 23:08
  • Could you show the command to print local variable "i"'s name "i$xxx"? I find it useful, but seems you have replaced the old answer with the newer one. – JimmyLin Jan 04 '14 at 23:50
  • My old answer is below my new one - it has always been there. Within the Manipulate, I have Print[i]; before the For-loop to print the variable name. – VF1 Jan 05 '14 at 00:06
  • @JimmyLin if you can see the value, why can't you evaluate your array[[value]] to see it? – Rojo Jan 05 '14 at 01:24
  • @VF1:I see that! Seems Print[] shows actual variable name only when it is not yet assigned a value. – JimmyLin Jan 05 '14 at 15:46