?
Solved

How to loop through number list to create sequence

Posted on 2008-06-13
4
Medium Priority
?
1,455 Views
Last Modified: 2008-06-13
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?
0
Comment
Question by:sterankin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21777892
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 Comment

by:sterankin
ID: 21778000
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() ;
                listBox1.Items.Add(seq);
                seq="";

}
else
 {    

     listBox1.Items.Add(listBox2.Items[i].ToString());

}
0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 500 total points
ID: 21778426
Try this function.

Regards,
Bruce
private string[] GetRanges(int[] RawData)
{
    List<string> reply = new List<string>();
    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)
            reply.Add(start.ToString());
        else
            reply.Add(string.Concat(start," - ", stop));
    }
 
    return reply.ToArray();
}

Open in new window

0
 

Author Comment

by:sterankin
ID: 21778806
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++)
            {
                listBox1.Items.Add (str[x]);
            }

Thanks for your help, always good to learn something new :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question