# Updating input parameter over iteration

**URL:** https://forum.kx.com/t/updating-input-parameter-over-iteration/13407
**Category:** Community Support
**Tags:** kdb-and-q, imported
**Created:** [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407 "2022-05-11T00:00:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mg4](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mg4](https://forum.kx.com/u/mg4)
#### Post date: [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/1 "2022-05-11T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/updating-input-parameter-over-iteration](https://learninghub.kx.com/forums/topic/updating-input-parameter-over-iteration)

Hi,

I was wondering if there’s a way to run a function over x number of iterations and also update a second value based on the function’s output.

For e.g.

summer:{[ID;y;z] y:y+z; ID:ID+1}  
{summer[x;y;z]}[;2;2]/[{x\<750};0]

Currently this runs for 750 iterations however y and z are set to 2 on input so y always equals 4, I have checked this using namespaces.

Is there a way to include another over somewhere so that y will run like 2+2=4, 4+2=6… for 750 iterations?

Any help appreciated!!

PS. The code is being used within peach, that’s why I can’t just use namespaces or a global variable to upsert into.

---

<div class="post-metadata">

### Author: ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)
#### Post date: [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/2 "2022-05-11T00:00:00Z")

</div>

Something more like:

q)750{(x[0]+1;x[1]+x[2];x[2])}/0

0 2 750 1500 2

Or use a dictionary for readability:

q)750{x[`ID]+:1;x[`y]:sum x`y`z;x}/`ID`y`z!0 0 2

ID| 750

y | 1500

z | 2

&nbsp;

---

<div class="post-metadata">

### Author: ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)
#### Post date: [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/3 "2022-05-11T00:00:00Z")

</div>

Is the `/` for of `do` what you are looking for?

[https://code.kx.com/q/ref/accumulators/#do](https://code.kx.com/q/ref/accumulators/#do")

```
q)750 +/2 2 
754
```

---

<div class="post-metadata">

### Author: ![mg4](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mg4](https://forum.kx.com/u/mg4)
#### Post date: [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/4 "2022-05-11T00:00:00Z")

</div>

Hey, thanks for the reply.

So I think the above is quite close however it only allows you run sum, not appending to iteration id as required.

Following similar to above:  
q)summer1:{[x;y] .m.x:x; x:x+y}

q)750 summer1[;2]/2;  
q).m.x  
1500

&nbsp;

I’m probably missing something obvious but is there a way to manipulate this to append to ID per iteration so that:  
q)summer:{[ID;y;z] y:y+z;.m.id:.m.id,ID;.m.y:y; ID:ID+1}

q).m.y

1500 (as above)

q).m.ID

750 (missing piece)

&nbsp;

Thanks again!

---

<div class="post-metadata">

### Author: ![mg4](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mg4](https://forum.kx.com/u/mg4)
#### Post date: [May 11, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/5 "2022-05-11T00:00:00Z")

</div>

The very one.

Fair play and thanks!

---

<div class="post-metadata">

### Author: ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)
#### Post date: [May 12, 2022, 12:00am UTC](https://forum.kx.com/t/updating-input-parameter-over-iteration/13407/6 "2022-05-12T00:00:00Z")

</div>

See article on the accumulator iteration operators:

[https://code.kx.com/q/ref/accumulators/](https://code.kx.com/q/ref/accumulators/")
