Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I tried to program a Sudoku solver when I suddenly ran into this problem:

I need a function which effectivley does this:

Updater[a_,b_,c_,Sudoku_]:=Sudoku[[a]][[b]]=c

Quasi an updater. But the code above won't work because the only thing I get is:

SetDelayed::setps: "{{Null,Null,8,1,7,6,Null,2,Null},{Null,4,Null,Null,Null,9,7,Null,Null},{Null,Null,Null,Null,Null,Null,Null,Null,Null},{Null,7,1,8,Null,Null,Null,Null,Null},{Null,Null,Null,Null,Null,Null,6,7,9},{Null,Null,Null,5,9,7,Null,8,1},{Null,Null,Null,Null,2,Null,Null,Null,Null},{Null,5,9,Null,Null,Null,Null,1,Null},{Null,1,4,6,5,Null,3,Null,Null}} in the part assignment is not a symbol."

The list in the error message is what I used as Sudoku in my Updater function.

share|improve this question

marked as duplicate by Mr.Wizard Aug 8 at 5:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
See specifically: (18393) –  Mr.Wizard Aug 8 at 5:47

2 Answers 2

This should solve your problem:

ClearAll@Updater
SetAttributes[Updater, HoldAll]
Updater[a_, b_, c_, Sudoku_] := Sudoku[[a, b]] = c

Sudoku = 
 {{Null, Null, 8, 1, 7, 6, Null, 2, Null}, {Null, 4, Null, Null, Null, 9, 7, Null, Null}};

Updater[1, 1, 99, Sudoku];

Sudoku

{{99, Null, 8, 1, 7, 6, Null, 2, Null}, {Null, 4, Null, Null, Null, 9, 7, Null, Null}}

share|improve this answer
    
Wow - that's it! Thanks. Would you care to elaborate on how that works? –  Levix Aug 7 at 16:29
    
You should download Leonid Shifrin's book at mathprogramming-intro.org and read Chapter 4.9 –  eldo Aug 7 at 16:54

I prefer to do it this way:

Clear@updater
SetAttributes[updater, HoldFirst];
updater[sudoku_, a_, b_, c_] := (sudoku[[a, b]] = c);

HoldFirst keeps the variable sudoku in an unevaluated form, which means it stays a variable symbol and does not become a variable value, in your specific case a list of values. a, b, c are evaluated, because only the first argument has attribute "HoldFirst"

Note: In the recommendations of Mma, they point out that users should write their symbols lowercase. Although this is only a convention, it is good programming practice to follow this advice. Or do you know for example that you can't use Beta as a variable

share|improve this answer

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