# How to loop through number list to create sequence

I have a listbox which contains a long list of numbers like this:

1123
1124
1125
1130
1131
1144
1145
1146
1155
1177
1179
1180
1181

I want to loop through this and return the sequences i.e. once the loop has completed the output would be:
1123 - 1125
1130 - 1131
1144 - 1146
1155
1177
1179 -1181

does this make sense?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
does this make sense? is a question, you have to answer.
What is your intention to do this?
What is the required output format (string? some kind of Range-Object?)

Regards,
Bruce
0

Author Commented:
Loop through one listbox which contains (this is a very short example):
1123
1124
1125
1130
1131
1144
1145
1146
1155
1177
1179
1180
1181

and display the following in another listbox:
1123 - 1125
1130 - 1131
1144 - 1146
1155
1177
1179 -1181

ie it should show the sequential unbroken ranges..

I tried creating a loop which looks at the next number in the list and seeing if it is equal to the previous number+1, its pretty close but not 100% right:

string seq=listBox2.Items[0].ToString();
for(int i =0; i<=listBox2.Items.Count;i++)
{
if(i<listBox2.Items.Count-1)
{

if((Convert.ToInt32(listBox2.Items[i].ToString())) + 1 == (Convert.ToInt32(listBox2.Items[i+1].ToString())))
{
seq  += " - " + listBox2.Items[i+1].ToString() ;
seq="";

}
else
{

}
0

Commented:
Try this function.

Regards,
Bruce
``````private string[] GetRanges(int[] RawData)
{
for (int i = 1; i < RawData.Length; i++)
{
int start = RawData[i - 1];

while (RawData[i - 1] + 1== RawData[i])
{
if (++i == RawData.Length)
break;
}

int stop = RawData[i - 1];

if (start == stop)
else
}

}
``````
0

Experts Exchange Solution brought to you by ConnectWise

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks Bruce!!
I see that is Generics?  I'm using VS 2003 which does not use Generics, but I tried it in 2005 and it worked ok.  I am unfamiliar with generics and it took me a while to work out how to use it correctly.

This is how I used it on a button click:

// create an int array to add the number to from the listbox
int[] arr =  new int [listBox2.Items.Count];
//add the numbers to the list box
for(int i=0;i<listBox2.Items.Count-1;i++)
{
arr[i]=Convert.ToInt32(listBox2.Items[i]);
}
//create a string array to hold the returned items
string[] str = new string[listBox2.Items.Count];
//call the method
str= GetRanges(arr);
//loop thourgh the string array and add to the list box
for (int x = 0; x < str.Length; x++)
{
}

Thanks for your help, always good to learn something new :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.