# dynamic index for array in while loop

**URL:** <https://community.unix.com/t/dynamic-index-for-array-in-while-loop/251222>\
**Category:** Shell Programming and Scripting\
**Created:** [October 13, 2009, 1:19pm UTC](https://community.unix.com/t/dynamic-index-for-array-in-while-loop/251222 "2009-10-13T13:19:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![weak\_code-fu](https://community.unix.com/letter_avatar/weak_code-fu/32/5_5575768a8748004e209b776fc1b2916d.png) [@weak\_code-fu](https://community.unix.com/u/weak_code-fu)\
**Post date:** [October 13, 2009, 1:19pm UTC](https://community.unix.com/t/dynamic-index-for-array-in-while-loop/251222/1 "2009-10-13T13:19:55Z")

</div>

Hi,

I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting an error "Missing -."

Here is the code I have:

```nohighlight
set array = `cat file | xargs -n3`
set counter=1
while ($counter <= $#arraysize)
  set a = ${array[$counter]}
  set b = ${array[$counter + 1]}
  set c = ${array[$counter + 2]}
end
 

```

I've also tried set b = ${array[`expr ${counter} + 2`]} or b = ${array[`expr $counter + 2`]} but they all give me the same "Missing -." error.

UPDATE: Ugh...nevermind, I figured out an ugly kludge. Sorry for bothering y'all. You can delete this thread if you want.
