tags:

views:

596

answers:

7

Say i have an array of values:

string[] text = new string[] {"val1", "val2", "val3", "val4", "val5"};

Then i have a basic loop:

for(int i=0;i<=30;i++) {
    Console.WriteLine(i + " = " + text[i])
}

Obviously this will cause an out of bounds exception, so what i want to do is when the counter reaches the upper bound of the array then go back to the start.

So

0 = val1

1 = val2

2 = val3

3 = val4

4 = val5

5 = val1

6 = val2

7 = val3

etc..

+13  A: 

You could use the modulus operator:

Console.WriteLine(i + " = " + text[i % 5])
yjerem
better to use text.Length, but +1 anyway ;-p
Marc Gravell
(should be text[i % 5], surely)
Marc Gravell
Thats great thanks, the name of the array was my fault I put string[i] in my post before editing.
Should be text[i % text.length] and you might want to change that 30 to be text.length * 6. That way in the future if you add an element you won't have to find those numbers and change them.
Samuel
Fixed the array name, thanks. I don't even know C# (I do know C/C++) so I didn't pay much attention to the code (just copied and pasted it from the question).
yjerem
+5  A: 

Try

for(int i=0;i<=30;i++)
{
    Console.WriteLine(i + " = " + string[i % 5])
}
AdamRalph
A: 

what? like forever?

bool run = true;
int i = 0;
string[] text = new string[] {"val1", "val2", "val3", "val4", "val5"};
while(run)
{
   Console.WriteLine(i + " = " + text[i])
   i++;
   if(i>=text.Length) i=0;
}
Al W
+1  A: 

The writeline should be:

Console.WriteLine(i + " = " + text[i%5]);
Mark Brittingham
modulus in the wrong place
AdamRalph
Oops...rushing to be first...
Mark Brittingham
I know what you mean, I was beaten by about 30 seconds ;-)
AdamRalph
lol - dayam - I'm off my game today! I've already gotten my 200 pts for the day though so I shouldn't even jumping in here when it is obvious that 100 people are going to get this one in seconds.
Mark Brittingham
I give up..."shouldn't *BE* jumping in here. Man, I'm going to bed :-/
Mark Brittingham
+10  A: 

Take the modulus of the array length:

for (int i = 0; i < 30; ++i)
{
    Console.WriteLine(i + " = " + text[i % text.Length]);
}
Jim Mischel
At least here you are using the Length property instead of hard-coding the length of the text array.
Kyle Walsh
+2  A: 

Shouldn't it be:

Console.WriteLine(i + " = " + text[i % text.length])

?

MK_Dev
+2  A: 

As a slightly less specific solution...

class Program
{
    static void Main(string[] args)
    {
        string[] text = new string[] { "val1", "val2", "val3", "val4", "val5" };

        int count = 0;
        foreach (string t in text.ContinuousLoopTo(30))
        {
            Console.WriteLine(count.ToString() + " = " + t);
            count++;
        }

        Console.ReadLine();
    }
}

public static class Extensions
{
    public static IEnumerable<T> ContinuousLoopTo<T>(this IList<T> list, int number)
    {
        int loops = number / list.Count;

        int i = 0;

        while (i < loops)
        {
            i++;

            foreach (T item in list)
            {
                yield return item;
            }
        }

        for (int j = 0; j < number % list.Count; j++)
        {
            yield return list[j];
        }
    }
}
Timothy Carter
A little bit overengineered, but I like it :) +1
Andrew Rollings
Absolutely, but overengineering can be fun :-P
Timothy Carter