4
$\begingroup$

Say I have a number $n_{max}$. I want a command to generate a table like

1 1
2 2
3 3
4 4
...
nmax nmax

where the second column is editable, and this table can be evaluated so that Mathematica treats the table as the set

replacement = { 1 -> 1, 2 -> 2, ...}

(The point of this is so that I can replace the original list using /.replacement).

So for example, after generating the table above, I can edit the second column as follows

1 A
2 B
3 C
4 D
...
nmax FF

and this output re-evaluates into set

replacement = { 1-> A, 2-> B, 3-> C, F-> D, ... , nmax-> FF}
$\endgroup$
6
  • 1
    $\begingroup$ What do you mean by editable? Just use Part. tmp = Transpose[{Range[4], Range[4]}]. tmp[[All, 2]] = {A, B, C, D}. But don't use capitalised symbols. $\endgroup$ Commented Feb 6, 2014 at 21:18
  • $\begingroup$ @MikeHoneychurch Probably something like a = RandomInteger[10,{5,2}]; TableView[Dynamic[a]]. WARNING: TableView in undocumented, it might not work, and it has already crashed my Mathematica ... save your work before trying! $\endgroup$
    – Szabolcs
    Commented Feb 6, 2014 at 21:23
  • 1
    $\begingroup$ @Szabolcs It is unclear (to me) whether he wants to edit like a spreadsheet or "edit" programmatically, but I thought the latter. TableView changed in V9. It is now a TableViewBox. Before that it was built from other boxes. $\endgroup$ Commented Feb 6, 2014 at 21:28
  • $\begingroup$ Sorry, I don't think I was unclear, but I wanted the former: just a table where the user can change any entry in the second column and have the table reevaluated as input. $\endgroup$
    – user12226
    Commented Feb 6, 2014 at 21:32
  • 1
    $\begingroup$ I think it would be better if you explained why you want this... This is really a cumbersome way of doing it (although it might make perfect sense to you) and you will probably end up having to write more patchwork code for when things go wrong. If you explained your intention, then perhaps we can point you to the right way of doing it. $\endgroup$
    – rm -rf
    Commented Feb 6, 2014 at 22:04

2 Answers 2

1
$\begingroup$

How about this. Generate a table of length nmax

nmax = 10; x = Table[{i, RandomInteger[]}, {i, 1, nmax}];

and use InputField to update the second column of the table

Column[{
  TableForm@
   Table[With[{i = i}, {x[[i, 1]], 
      InputField[Dynamic[x[[i, 2]]], FieldSize -> Tiny]}], {i, 1, 
     nmax}],
  replacement = Dynamic@x;
  }]

( The column is there just in case you want to remove the ; and view the current value of replacement.)

Mathematica graphics

The symbol replacement is updated whenever the data in the second column is changed, and you can access a set of replacement rules using:

{#[[1]] -> #[[2]]} & /@ replacement[[1]]
(* {{1 -> 2}, {2 -> 3}, {3 -> 4}, {4 -> 5}, {5 -> 6}, {6 -> 0}, {7 -> 1}, {8 -> 0}, {9 -> 0}, {10 -> 1}} *)
$\endgroup$
0
$\begingroup$
nMax = 10;
Block[{tab, boxes},
 tab = Table[
   {i, InputField[i]}
   ,
   {i, nMax}
   ];

 boxes = ToBoxes@TableForm[tab];

 boxes[[2]] = 
  Function[e, Print["replacement is updated"]; 
   replacement = #[[1]] -> #[[2, 1]] & /@ e];

 Cell[BoxData@boxes
   ,
   "Input"
   ] // CellPrint
 ]

Prints an input cell that looks like

enter image description here

which evaluates to

{1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5}

and sets the variable replacement to be the result of evaluation above.

This is very similar to bobthechemists answer, but it relies less on Dynamic, which I think is nice. A downside of the code is that it assumes that the box generated by ToBoxes@TableForm@tab is a TagBox with a certain structure. That is why I used box expression in my previous answer, but the new code looks much cleaner.

I like the use of InputFields suggested by bobthechemist. Note that this allows one to "tab" through the table.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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