473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

splitting a collection of numbers

Hi,

I have a situation where I have a collection that holds numbers.
Mostly they are concurrent eg. 125801-125899 but sometimes they are
not eg. 125801-125899, 195301-399. Is there any way to split the
numbers and create another collection based on where the numbers
change, that is one colletion for the 125801-125899 range and another
for the 195301-399 range.

Any help much appreciated.

Cheers
David
Jun 27 '08 #1
3 1233
You haven't said what type of collection your 'numbers' are in, or, for that
matter, what type of 'numbers' you are dealing with, so, so let's assume,
for the purpose of the exercise, that you are dealing with a generic-based
List(Of Integer).

I also assume that you mave miss-typed the second range and that it actually
should be 195301-195399.

So, if you sort your collection you end up with a list that looks like:

125801
125802
...
125898
125899
195301
195302
...
195398
195399

And you want to split the collection between 125899 and 195301 so that you
have two new collections.

If you know, in advance, how many new collections you will have then you can
declare the appropriate number of collections and then fill them
appropriately:

_existingcollection.Sort()

Dim _newcollection1 = New List(Of Integer)
Dim _newcollection2 = New List(Of Integer)

Dim _currentcollection = _newcollection1

_currentcollection.Add(_existingcollection(0))

For _i = 1 To _existingcollection.Count - 1
If _existingcollection(_i) < _existingcollection(_i - 1) Then
_currentcollection = _newcollection2
_currentcollection.Add(_existingcollection(_i))
Next

When the 'range break' is encountered, the reference to the new collection
that is being used is changed.

The result is that _newcollection1 contains 1235801 to 135899 inclusive and
_newcollection2 contains 195301 to 195399 inclusive.

But .... you will run into problems if you do not know in advance how many
ranges you have.

To cater for this, a better mousetrap would be:

_existingcollection.Sort()

Dim _newcollections = New List(Of List Of(Integer))

Dim _newcollection = New List(Of Integer)

_newcollection.Add(_existingcollection(0))

For _i = 1 To _existingcollection.Count - 1
If _existingcollection(_i) < _existingcollection(_i - 1) Then
_newcollections.Add(_newcollection)
_newcollection = New List(Of Integer)
End If
_newcollection.Add(_existingcollection(_i))
Next

_newcollections.Add(_newcollection)

This results in a collection of ranges with each range containing 1 or more
consectutive integers.

You can obtain the number of ranges with:

_newcollections.Count

You can obtain the actual ranges with:

For Each _range in _newcollections
Console.Writeline("{0} - {1}", _range(0), _range(_range.Count - 1))
Next

or:

For _i = 0 to _newcollections.Count - 1
Dim _range = _newcollections(_i)
Console.Writeline("Range {0} contains {1} - {2}", _i + 1, _range(0),
_range(_range.Count - 1))
Next

This type of thing can be easliy modified so that, instead of 'storing' with
lists of numbers, you have a collection of Range objects that each contain
information about the range, for example, it's first and last values.
<dj****@internode.on.netwrote in message
news:16**********************************@y22g2000 prd.googlegroups.com...
Hi,

I have a situation where I have a collection that holds numbers.
Mostly they are concurrent eg. 125801-125899 but sometimes they are
not eg. 125801-125899, 195301-399. Is there any way to split the
numbers and create another collection based on where the numbers
change, that is one colletion for the 125801-125899 range and another
for the 195301-399 range.

Any help much appreciated.

Cheers
David
Jun 27 '08 #2
Correction!

The test inside each of the loops demos=nstrated should read ...

If _existingcollection(_i) - 1 < _existingcollection(_i - 1) Then ...
"Stephany Young" <noone@localhostwrote in message
news:en**************@TK2MSFTNGP05.phx.gbl...
You haven't said what type of collection your 'numbers' are in, or, for
that matter, what type of 'numbers' you are dealing with, so, so let's
assume, for the purpose of the exercise, that you are dealing with a
generic-based List(Of Integer).

I also assume that you mave miss-typed the second range and that it
actually should be 195301-195399.

So, if you sort your collection you end up with a list that looks like:

125801
125802
...
125898
125899
195301
195302
...
195398
195399

And you want to split the collection between 125899 and 195301 so that you
have two new collections.

If you know, in advance, how many new collections you will have then you
can declare the appropriate number of collections and then fill them
appropriately:

_existingcollection.Sort()

Dim _newcollection1 = New List(Of Integer)
Dim _newcollection2 = New List(Of Integer)

Dim _currentcollection = _newcollection1

_currentcollection.Add(_existingcollection(0))

For _i = 1 To _existingcollection.Count - 1
If _existingcollection(_i) < _existingcollection(_i - 1) Then
_currentcollection = _newcollection2
_currentcollection.Add(_existingcollection(_i))
Next

When the 'range break' is encountered, the reference to the new collection
that is being used is changed.

The result is that _newcollection1 contains 1235801 to 135899 inclusive
and _newcollection2 contains 195301 to 195399 inclusive.

But .... you will run into problems if you do not know in advance how many
ranges you have.

To cater for this, a better mousetrap would be:

_existingcollection.Sort()

Dim _newcollections = New List(Of List Of(Integer))

Dim _newcollection = New List(Of Integer)

_newcollection.Add(_existingcollection(0))

For _i = 1 To _existingcollection.Count - 1
If _existingcollection(_i) < _existingcollection(_i - 1) Then
_newcollections.Add(_newcollection)
_newcollection = New List(Of Integer)
End If
_newcollection.Add(_existingcollection(_i))
Next

_newcollections.Add(_newcollection)

This results in a collection of ranges with each range containing 1 or
more consectutive integers.

You can obtain the number of ranges with:

_newcollections.Count

You can obtain the actual ranges with:

For Each _range in _newcollections
Console.Writeline("{0} - {1}", _range(0), _range(_range.Count - 1))
Next

or:

For _i = 0 to _newcollections.Count - 1
Dim _range = _newcollections(_i)
Console.Writeline("Range {0} contains {1} - {2}", _i + 1, _range(0),
_range(_range.Count - 1))
Next

This type of thing can be easliy modified so that, instead of 'storing'
with lists of numbers, you have a collection of Range objects that each
contain information about the range, for example, it's first and last
values.
<dj****@internode.on.netwrote in message
news:16**********************************@y22g2000 prd.googlegroups.com...
>Hi,

I have a situation where I have a collection that holds numbers.
Mostly they are concurrent eg. 125801-125899 but sometimes they are
not eg. 125801-125899, 195301-399. Is there any way to split the
numbers and create another collection based on where the numbers
change, that is one colletion for the 125801-125899 range and another
for the 195301-399 range.

Any help much appreciated.

Cheers
David
Jun 27 '08 #3
On Jun 16, 4:07*pm, "Stephany Young" <noone@localhostwrote:
Correction!

The test inside each of the loops demos=nstrated should read ...

* If _existingcollection(_i) - 1 <*_existingcollection(_i - 1) Then ....

"Stephany Young" <noone@localhostwrote in message

news:en**************@TK2MSFTNGP05.phx.gbl...
You haven't said what type of collection your 'numbers' are in, or, for
that matter, what type of 'numbers' you are dealing with, so, so let's
assume, for the purpose of the exercise, that you are dealing with a
generic-based List(Of Integer).
I also assume that you mave miss-typed the second range and that it
actually should be 195301-195399.
So, if you sort your collection you end up with a list that looks like:
*125801
*125802
*...
*125898
*125899
*195301
*195302
*...
*195398
*195399
And you want to split the collection between 125899 and 195301 so that you
have two new collections.
If you know, in advance, how many new collections you will have then you
can declare the appropriate number of collections and then fill them
appropriately:
*_existingcollection.Sort()
*Dim _newcollection1 = New List(Of Integer)
*Dim _newcollection2 = New List(Of Integer)
*Dim _currentcollection = _newcollection1
*_currentcollection.Add(_existingcollection(0))
*For _i = 1 To _existingcollection.Count - 1
* *If _existingcollection(_i) <*_existingcollection(_i - 1) Then
_currentcollection = _newcollection2
* *_currentcollection.Add(_existingcollection(_i))
*Next
When the 'range break' is encountered, the reference to the new collection
that is being used is changed.
The result is that _newcollection1 contains 1235801 to 135899 inclusive
and _newcollection2 contains 195301 to 195399 inclusive.
But .... you will run into problems if you do not know in advance how many
ranges you have.
To cater for this, a better mousetrap would be:
*_existingcollection.Sort()
*Dim _newcollections = New List(Of List Of(Integer))
*Dim _newcollection = New List(Of Integer)
*_newcollection.Add(_existingcollection(0))
*For _i = 1 To _existingcollection.Count - 1
* *If _existingcollection(_i) <*_existingcollection(_i - 1) Then
* * _newcollections.Add(_newcollection)
* * _newcollection = New List(Of Integer)
* *End If
* *_newcollection.Add(_existingcollection(_i))
*Next
*_newcollections.Add(_newcollection)
This results in a collection of ranges with each range containing 1 or
more consectutive integers.
You can obtain the number of ranges with:
*_newcollections.Count
You can obtain the actual ranges with:
*For Each _range in _newcollections
* *Console.Writeline("{0} - {1}", _range(0), _range(_range.Count - 1))
*Next
or:
*For _i = 0 to _newcollections.Count - 1
* *Dim _range = _newcollections(_i)
* *Console.Writeline("Range {0} contains {1} - {2}", _i + 1, _range(0),
_range(_range.Count - 1))
*Next
This type of thing can be easliy modified so that, instead of 'storing'
with lists of numbers, you have a collection of Range objects that each
contain information about the range, for example, it's first and last
values.
<djc...@internode.on.netwrote in message
news:16**********************************@y22g2000 prd.googlegroups.com...
Hi,
I have a situation where I have a collection that holds numbers.
Mostly they are concurrent eg. 125801-125899 but sometimes they are
not eg. 125801-125899, 195301-399. Is there any way to split the
numbers and create another collection based on where the numbers
change, that is one colletion for the 125801-125899 range and another
for the 195301-399 range.
Any help much appreciated.
Cheers
David- Hide quoted text -

- Show quoted text -
Thank you for that, very much appreciated

David
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mark Light | last post by:
Hi, I have a string e.g. 'C6 H12 O6' that I wish to split up to give 2 strings 'C H O' and '6 12 6'. I have played with string.split() and the re module - but can't quite get there. Any help...
10
by: Angelo Secchi | last post by:
Hi, I have string of numbers and words like ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n' and I would like to split (I'm using string.split()) it using comma as separator but I do not want to...
1
by: Sugapablo | last post by:
I have a column named "LIST" in a table with strings like the following: 151231-1002-02-1001 151231-1001-02-1001 151231-1002-02-1002 151231-1003-02-1001 etc.... What I'd like to do is...
11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
7
by: Eric McGraw | last post by:
In Firefox, as in most regular expression engines, the split function works like this: > "His name is <name>. His email address is <email>.".split(/<(.*?)>/) returns so that the text pattern...
4
by: nikila | last post by:
Hi, I am trying to split large xml files to smaller xml files using c#.net. can you please provide any sample code for this? I have to split the file if the size is more than 10 MB. Also, xml...
1
by: Denis | last post by:
I have a date field in a database. I have been given forms to be printed out. The problem is that the form requires the date to be printed out in 3 seperate parts. In the form day month and year....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.