# Incrementing a nested list element

I've been wanting to increment a nested list element of which I know the "coordinates". I thought of the following procedure, using Apply with Part.

test = Table[10 h + 5 i + j, {h, 0, 3}, {i, 0, 1}, {j, 0, 4}];
pos = {4, 2, 3};
Increment[test[[##]] & @@ pos];


Yet it seems that test[[##]] & @@ pos is treated like a different variable. When I print

test[[##]] & @@ pos
test


I can see that test[[##]] & @@ pos gives an incremented result, but test itself is not changed. What would be a nice and efficient way of incrementing a nested list element, saving the new value of the whole list ?

## Edit

So in the light of the first comments, let me say more about what I would like to achieve :

I have a nested list test which can be of any depth. I have a list pos whose length is equal to

Depth[test]-1


The goal is to increment the element at the position pos in test (I guess by using Apply) and return the incremented nested list. My method below increments well but then does not store the new value in the list.

-
test[[pos]]=test[[pos]] + 1 ? – b.gatessucks Aug 1 '12 at 8:24
what do you want your final result to look like? – Mike Honeychurch Aug 1 '12 at 8:29
test[[pos]] does not work because pos is a list. I would like my final result to be the same nested list but with one of the element (given by ´pos) incremented by one. – Pschoofs Aug 1 '12 at 8:31

Try this:

test[[Sequence@@pos]]++

-
Works and returns the incremented list ! Thanks a lot ;) – Pschoofs Aug 1 '12 at 8:47
+1 Nice find ;-) – Vitaliy Kaurov Aug 1 '12 at 8:55

Why to go through the hustle defining pos manually and then using @@ ? With the same effort you can just put coordinates explicitly inside [[...]]

test[[4, 2, 3]]++

-
The idea is to do it dynamically for plenty of pos. And technically test can be of any dimension, and pos should be of the corresponding length. – Pschoofs Aug 1 '12 at 8:38
@Pschoofs Could you please show then the more general use/goal you are trying to achieve? – Vitaliy Kaurov Aug 1 '12 at 8:39
Check the edit, I hope I could explain myself a little better ;) – Pschoofs Aug 1 '12 at 8:45
@Pschoofs thanks for the update. But I think if you can define and dynamically update pos as a separate statement, you can also easily do it inside [[...]] too ;-) – Vitaliy Kaurov Aug 1 '12 at 8:53

If you "must" use Apply you can use it as

Increment[test[[##]]] & @@ pos
(* 37 *)


or

test[[##]]++ & @@ pos
(* 37 *)


which gives

test
(* {{{0, 1, 2, 3, 4}, {5, 6, 7, 8, 9}},
{{10, 11, 12, 13, 14}, {15, 16, 17, 18, 19}},
{{20, 21, 22, 23, 24}, {25, 26, 27, 28, 29}},
{{30, 31, 32, 33, 34}, {35, 36, 38, 38, 39}}} *)
`
-