0

Why does this module return 11 (as expected) when 'Accept' is clicked:

blist = {{"Accept", 11}, {"Cancel", $Failed}};
thisWorks[] := Module[{buttons = {}},
   Map[AppendTo[buttons, Button[#[[1]], DialogReturn[#[[2]]]]]&, blist];
   Return@DialogInput[buttons, Modal->True]
  ];

while this module does not?

thisFails[] := Module[{i, n, buttons = {}},
   For[i = 1; n = Length@blist, i <= n, i++,
    AppendTo[buttons,
     Button[blist[[i, 1]], DialogReturn[blist[[i, 2]]]]]]; 
   Return@DialogInput[buttons, Modal->True]
  ];

Hold@DialogReturn does not work either, in fact, thisFails[] does not return if used.

(Not that anyone would EVER want to use a For loop, of course :)

M. Robinson
  • 161
  • 7
  • Take a look at InputForm @ buttons instead of DialogInput. For/Do/Table are not able to inject values of iterators into held expressions, and Button is HoldRest. p.s. Return is redundant. – Kuba Mar 27 '18 at 06:45
  • Linked topic should answer your doubts, let me know if you disagree with closing. – Kuba Mar 27 '18 at 06:46
  • I don't really understand the linked example, but agree that it seems to be asking a related question. Just to put my mind at rest -- there is no way to use a For loop here? If there is a way, I'd like to know what it is. – M. Robinson Mar 27 '18 at 17:48
  • The linked question is different but check the answer. You can use For, add mentioned trick in for's body: With[{i=i... – Kuba Mar 27 '18 at 18:16
  • P.s use Table instead of for + append – Kuba Mar 27 '18 at 18:17
  • OK Kuba, thanks. With[{k = i}... works. Go ahead and kill it if you like. – M. Robinson Mar 27 '18 at 18:53
  • No, marking as duplicate has a purpose of leaving a road sign for future visitors. – Kuba Mar 27 '18 at 19:16

0 Answers0