2

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";
dataA1

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?

Update

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

Murta
  • 26,275
  • 6
  • 76
  • 166
  • @LeonidShifrin tks!.. Should I delete this? – Murta Jan 19 '13 at 15:41
  • 2
    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
  • 1
    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
  • Yes! It works. For your and Mr.Wizard. For me this is the best simpler application of Upvalues. Tks! – Murta Jan 26 '13 at 00:57
  • Good to know it worked for you! – Leonid Shifrin Jan 26 '13 at 10:17

1 Answers1

3

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"}}.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
andre314
  • 18,474
  • 1
  • 36
  • 69
  • 3
    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 – andre314 Jan 19 '13 at 15:59