Solved

convert coldfusion to vb.net

Posted on 2014-07-22
3
289 Views
Last Modified: 2014-07-22
This code below is exactly what I am trying to do in vb.net.
I need help converting it to understand it....

<!--- always sort first so the ranges make sense --->
<cfset numbers = listToArray("1,2,3,4,6,7,8,9,10,11,12,15,19,21,22")>
<cfset arraySort(numbers, "numeric")>

<cfset startAt = 1>
<cfset maxPosition = arrayLen(numbers)>
<cfset ranges = []>

<cfloop from="1" to="#maxPosition#" index="pos">
        <!--- check difference between current and next number --->
	<cfset offset = pos lt maxPosition ? 1 : 0>
	<cfset gap = numbers[pos+offset] - numbers[pos]>

	<!--- there was a jump in sequence OR this is the last element --->
	<cfif gap gt 1 OR pos eq maxPosition>
		<cfif startAt lt pos>
			<!--- range --->
			<cfset arrayAppend(ranges, numbers[startAt] &"-"& numbers[pos])>
		<cfelse>
			<!--- single value --->
			<cfset arrayAppend(ranges, numbers[pos])>
		</cfif>
		<cfset startAt = pos + 1>
	</cfif>
</cfloop>

List of Ranges: <cfdump var="#arrayToList(ranges, ',')#">

Open in new window

0
Comment
Question by:taviaf
[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
3 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40211766
Here you go:
        Dim numbers() As Integer
        Dim numberss() As String
        Dim ranges() As String
        Dim list As String = "1,2,3,4,6,7,8,9,10,11,12,15,19,21,22"
        Dim startAt, maxPosition, pos, offset, gap As Integer
        ReDim ranges(0)
        ranges(0) = ""
        numberss = list.Split(",")
        startAt = 0
        maxPosition = numberss.Length - 1
        ReDim numbers(maxPosition)
        For pos = 0 To maxPosition
            numbers(pos) = CInt(numberss(pos))
        Next
        Array.Sort(numbers)
        For pos = 0 To maxPosition
            If pos < maxPosition Then offset = 1 Else offset = 0
            gap = numbers(pos + offset) - numbers(pos)
            If gap > 1 Or pos = maxPosition Then
                If ranges(0) <> "" Then Array.Resize(ranges, ranges.Length + 1)
                If startAt < pos Then
                    ranges(ranges.Length - 1) = CStr(numbers(startAt)) & "-" & CStr(numbers(pos))
                Else
                    ranges(ranges.Length - 1) = CStr(numbers(pos))
                End If
                startAt = pos + 1
            End If
        Next
        MsgBox(String.Join(",", ranges))

Open in new window

0
 

Author Closing Comment

by:taviaf
ID: 40212109
Would give an A+ if I could..... Thank you a million times
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40212120
Lol thanks, no problem :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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