Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
How to Set parts of indexed lists?

For better organization of my code, sometime I use data objects in the form:

data["A", "1"] = {{1, 2}, {3, 4}}

instead of

dataA1 = {{1, 2}, {3, 4}};

One of the advantages is when working with Module, because I can declare data just one time, and I can use data["A","1"], data["A","2"], data["B","1"] and so on, with limited scope.

Everything works fine except for one detail. With data["A", "1"] I can't perform operations like:

data["A", "1"] = {{1, 2}, {3, 4}}
data["A", "1"][[All, 2]] = "X"

to change data["A", "1"] to {{1, "X"}, {3, "X"}}

I got the error:

Set::setps: data["A", "1"] in the part assignment is not a symbol

When I try with:

dataA1 = {{1, 2}, {3, 4}};
dataA1[[All, 2]] = "X";

I have no problem and get {{1, "X"}, {3, "X"}} as expected.

There is nome way to perform this kind of operation in data["A", "1"] object?


The question indicated as duplicated do not handle the case: data["A", "1",...] but just data["A"]

share|improve this question

marked as duplicate by Leonid Shifrin, Mr.Wizard Jan 19 '13 at 16:33

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 also this Mathgroup thread – Leonid Shifrin Jan 19 '13 at 15:32
@LeonidShifrin tks!.. Should I delete this? – Murta Jan 19 '13 at 15:41
Not sure about deletion. It may be better to close this but keep it alive. If you make a slightly more specific / descriptive title, it may drive future visitors to that question. I take it that you tried to find something on the topic before asking, and did not find that one. And I only found it because I remember answering it. So, right now I am for closing, let's see what others think. – Leonid Shifrin Jan 19 '13 at 15:46
@LeonidShifrin hi!.. I'm studying Upvalues and I just saw that your answer and Mr.Wisard too do not apply to my case data["A", "1"] they work for just one arg function. It's not the case to reopen it? Or it's simple to extend it for data[__] case? – Murta Jan 25 '13 at 14:03
Hello! In my answer to that question, simply replace everywhere sym[index_] with sym[indices__] (of course, consistently across the bodies of the functions), and it should work, I think. Let me know if it does not. As for the answer of @Mr.Wizard, I suppose that should work for it too. – Leonid Shifrin Jan 25 '13 at 14:26

1 Answer 1

You may use:

 data["A", "1"] = ReplacePart[data["A", "1"], {_, 2} -> "X"]

It transforms data["A", "1"] from {{1, 2}, {3, 4}} to {{1, "X"}, {3, "X"}}.

share|improve this answer
The problem with this is that ReplacePart copies entire expression, so a single element assignment using ReplacePart will not have a constant time complexity. For most applications, this will be a disaster. – Leonid Shifrin Jan 19 '13 at 15:38
I agree that this should only be used when computing time is insignifiant – andre Jan 19 '13 at 15:59

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