Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example:
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
%IDC_Button = 500
%IDC_TextBox = 501
Global hDlg As Dword
Global qFreq, qStart, qStop As Quad
Function PBMain() As Long
Dialog Default Font "Tahoma", 12, 1
Dialog New Pixels, 0, "PowerBASIC",300,300,200,100, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Convert", 50,10,100,25
Control Add TextBox, hDlg, %IDC_TextBox,"1,2,3,4,7,8,9",10,45,170,25
' Control Add TextBox, hDlg, %IDC_TextBox,"1-4,7,9-10,14,17",10,45,170,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, i As Long
Select Case Cb.Msg
Case %WM_InitDialog
QueryPerformanceFrequency qFreq
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button, %IdOk
Control Get Text hDlg, %IDC_TextBox To temp$
If InStr(temp$,"-") Then Expand_Gary(temp$) Else Collapse(temp$)
Control Set Text hDlg, %IDC_TextBox, temp$
Dialog Set Text hDlg, Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
End Select
End Select
End Function
Sub Collapse(temp$)
'1,2,3,4,7,9,10,14,17
Local i,j,k,iCount,iStart,iEnd As Long, result$, tmp$
iCount = ParseCount(temp$,",")
ReDim T(1 To iCount) As Long
For i = 1 To iCount : T(i) = Val(Parse$(temp$,",",i)) : Next i
For i = 1 To UBound(T)
If i = 1 Then
iStart = T(i) : iEnd = iStart
ElseIf T(i) = T(i-1) + 1 Then
iEnd = T(i)
ElseIf T(i) > T(i-1) + 1 Then
result$ += Trim$(Str$(iStart)) + IIf$(iStart=iEnd, ",", "-" + Trim$(Str$(iEnd)) + ",")
iStart = T(i) : iEnd = iStart
End If
Next i
result$ += Trim$(Str$(iStart)) + IIf$(iStart=iEnd, "", "-" + Trim$(Str$(iEnd)) + ",")
temp$ = Trim$(result$,",")
End Sub
Sub Expand(temp$)
'1-4,7,9-10,14,17
Local i,j,iStart,iEnd As Long, result$, T$
For i = 1 To ParseCount(temp$,",")
T$ = Parse$(temp$,",",i)
If InStr(T$,"-") Then
iStart = Val(Parse$(T$,"-",1))
iEnd = Val(Parse$(T$,"-",2))
For j = iStart To iEnd : result$ += Trim$(Str$(j)) + "," : Next j
Else
result$ += T + ","
End If
Next
temp$ = Trim$(result$,",")
End Sub
http://www.garybeene.com/sw/gbsnippets.htm