You are on page 1of 7

Attribute VB_Name = "Mudule2"

'********************************************************
' Add a string to the tail of an array
'********************************************************
Public Sub Add_to_list(ByRef Curr_Arr() As String, _
ByVal mila As String)
Dim lng As Integer
ReDim Preserve Curr_Arr(UBound(Curr_Arr) + 1)
lng = UBound(Curr_Arr)
Curr_Arr(lng) = mila
End Sub
'****************************************************************
'********************************************************
' Is true if a string exists in an array
'********************************************************
Public Function In_list(ByVal mila As String, _
ByRef My_Arr() As String) _
As Boolean
Dim Cnt As Integer
Dim found As Boolean

found = False

Cnt = 0
Do
If mila = My_Arr(Cnt) Then found = True
Cnt = Cnt + 1
Loop Until ((found = True) Or (Cnt > UBound(My_Arr)))
In_list = found
End Function
'********************************************************
' Add a single to array of singles
'
Public Sub Add_to_Slist(ByRef Lst() As Single, ByVal n As Single)
ReDim Preserve Lst(UBound(Lst) + 1)
Lst(UBound(Lst)) = n
End Sub
'********************************************************

' Add string to array ONLY IF it does not appear in array


'****************************************************************
Public Sub Add_new_to_list(ByRef Curr_Arr() As String, _
ByVal mila As String)
If Not In_list(mila, Curr_Arr) Then Add_to_list Curr_Arr, mila
End Sub
'#################################################################
' Sorts an array of strings According the number of
' Words in string.
' The Sort Is in DESCENDING ORDER.
' ===============================================================
Public Sub SortArr_Words_Down(ByRef Lst() As String)

Dim temp As String


Dim i As Integer
Dim j As Integer

For i = 1 To UBound(Lst) - 1
For j = i + 1 To UBound(Lst)
If Words_Count(Lst(j)) > Words_Count(Lst(i)) Then
Swap_val Lst(j), Lst(i)
End If
Next j
Next i
End Sub
'#################################################################
' Sorts an array of strings According the number of
' Words in string.
' The Sort Is in ASCENDING ORDER.
' ===============================================================
Public Sub SortArr_Words_Up(ByRef Lst() As String)

Dim temp As String


Dim i As Integer
Dim j As Integer

For i = 1 To UBound(Lst) - 1
For j = i + 1 To UBound(Lst)
If Words_Count(Lst(j)) < Words_Count(Lst(i)) Then
Swap_val Lst(j), Lst(i)
End If
Next j
Next i
End Sub
'#################################################################
' Bubbles sort
' Insertion sort is NOT faster for singles. checked !
Public Sub Sort_Num_Arr(ByRef Lst() As Single)

Dim temp As Single


Dim i As Integer
Dim j As Integer

If UBound(Lst) > 1 Then


For i = 1 To UBound(Lst) - 1
For j = i + 1 To UBound(Lst)
If Lst(j) < Lst(i) Then
temp = Lst(j)
Lst(j) = Lst(i)
Lst(i) = temp
End If
Next j
Next i
End If
End Sub
'#################################################################
' CHECKED OK !!
' Insertion sort. Sorts arrays of strings. Faster then SortRrr
'
Public Sub Sort_Arr(ByRef Lst() As String)
Dim arr() As String
Dim n As Long
Dim j As Long
Dim i As Long ' counter for shifting

If UBound(Lst) > 1 Then


arr = Lst
ReDim Lst(UBound(arr))
Lst(1) = arr(1)
For n = 2 To UBound(arr) ' counter of arr array
j = 0
Do
j = j + 1
Loop Until arr(n) < Lst(j) Or j = n

If j < n Then ' push from j to max amd update max


For i = n - 1 To j Step -1
Lst(i + 1) = Lst(i)
Next i
End If
Lst(j) = arr(n) ' Do it any case
Next n
End If
End Sub
'#################################################################
Public Sub SortArr_Dec(ByRef Lst() As String)

Dim temp As String


Dim i As Integer
Dim j As Integer

If UBound(Lst) > 1 Then


For i = 1 To UBound(Lst) - 1
For j = i + 1 To UBound(Lst)
If Lst(j) > Lst(i) Then
Swap_val Lst(j), Lst(i)
End If
Next j
Next i
End If
End Sub
'########################################################
' Swap Values between 2 strings
' ===============================================================
Public Sub Swap_val(ByRef s1 As String, ByRef s2 As String)
Dim temp As String
temp = s1
s1 = s2
s2 = temp
End Sub
'##########################################################################
Private Function Get_Longest_Word(ByRef Lst() As String) As Integer

Dim Mx As Integer
Dim n As Integer

Mx = 0
For n = 1 To UBound(Lst)
If Len(Lst(n)) > Mx Then Mx = Len(Lst(n))
Next n
Get_Longest_Word = Mx
End Function
'##########################################################################
Private Function Get_Line(ByVal num1 As Integer, _
ByVal num2 As Integer, _
ByRef Lst() As String, _
ByVal Mx As Integer, _
ByVal Sp As Byte)
Dim n As Integer
Dim temp As String
Dim mila As String

temp = String(Sp, " ")


For n = num1 To num2
mila = Lst(n)
Add_Spc mila, Mx + 2
temp = temp + mila
Next n
Get_Line = temp
End Function
'############################################################

Public Sub Arrange_Groups(ByRef Lst1() As String, ByVal Sp As Byte)

Dim Mx As Integer ' Length of longest word in array


Dim Sof As Integer ' Dimension of original array
Dim i As Integer ' Counter
Dim st As Integer ' Start point
Dim en As Integer ' End point
Dim Sheerit As Integer
Dim K As Byte ' Number of words in a row
Dim Lst_old() As String

ReDim Lst_old(0)
Lst_old = Lst1
ReDim Lst1(0)
If UBound(Lst_old) > 0 Then
Mx = Get_Longest_Word(Lst_old)
K = (45 - Sp) \ (Mx + 1) ' Consider 2 spaces
' --------------------------------------------------------------------
' 70 !! but in other programs it is 78 !!
' ( Mx +2 ) in other programs !!
' --------------------------------------------------------------------
Sof = UBound(Lst_old)
For i = 1 To Sof \ K
st = (i - 1) * K + 1 ' START OF RANGE
en = st + K - 1 ' END OF RANGE
Add_to_list Lst1, Get_Line(st, en, Lst_old, Mx, Sp)
Next i
Sheerit = Sof Mod K
If Sheerit > 0 Then
st = Sof - Sheerit + 1
Add_to_list Lst1, Get_Line(st, Sof, Lst_old, Mx, Sp)
End If
End If
End Sub
'##########################################################################

' Lst3 holds strings which appear in Lst1 AND in LST2


'
----------------------------------------------------------------------------------
--
'
Public Sub Common_Links(ByRef Lst1() As String, _
ByRef Lst2() As String, _
ByRef Lst3() As String)
Dim n As Integer

ReDim Lst3(0)
If UBound(Lst1) > 0 And UBound(Lst2) > 0 Then
For n = 1 To UBound(Lst1)
If In_list(Lst1(n), Lst2) Then Add_to_list Lst3,
Lst1(n)
Next n
End If
End Sub
'##########################################################################
Public Function Equal_Lists(ByRef Lst1() As String, _
ByRef Lst2() As String) As
Boolean

Dim arr1() As String


Dim Arr2() As String

ReDim arr1(0)
ReDim Arr2(0)
In_First_Only Lst1, Lst2, arr1
In_First_Only Lst2, Lst1, Arr2
Equal_Lists = UBound(arr1) = 0 And UBound(Arr2) = 0
End Function
'##########################################################################
'
' Lst3 holds strings which appear in Lst1 AND NOT in LST2
'
----------------------------------------------------------------------------------
--------
'
Public Sub In_First_Only(ByRef Lst1() As String, _
ByRef Lst2() As String, _
ByRef Lst3() As String)

Dim n As Integer

If UBound(Lst1) > 0 Then


If UBound(Lst2) > 0 Then
For n = 1 To UBound(Lst1)
If Not In_list(Lst1(n), Lst2) Then Add_to_list Lst3,
Lst1(n)
Next n
Else ' Lst2 is empty. transfer whole Lst1 to
Lst3
Lst3 = Lst1
End If
End If
End Sub
'##########################################################################

' Prints an array of type string in file named Curr_File


'
----------------------------------------------------------------------------------
---
Public Sub Print_Lst(ByRef Lst() As String, ByRef Curr_File As String)
Dim n As Integer

Open Curr_File For Output As #9


If UBound(Lst) < 1 Then
Print #9, "No Elements in Array."
Else
For n = 1 To UBound(Lst)
Print #9, Lst(n)
Next n
End If
Close #9
End Sub
'**************************************************************

' Creates a string from an array of strings


'---------------------------------------------------------------------
Public Function Arr2str(ByRef Lst() As String) As String
Dim n As Integer
Dim temp As String

temp = ""
Arr2str = ""
If UBound(Lst) > 0 Then
For n = 1 To UBound(Lst)
temp = temp + " " + Lst(n)
Next n
End If
Arr2str = Trim(temp)
End Function
'**************************************************************
' Converts a string into an array. Each word in a link
' NEW VERSION. WORKS 83% FASTER THEN OLD ONE.
Public Sub Str2Arr(ByVal s As String, ByRef Lst() As String)
Dim n As Long
Dim temp() As String

s = Trim(Swap_Str(s, " ", " "))


temp = Split(s)
ReDim Lst(0)
For n = 0 To UBound(temp)
Add_to_list Lst, temp(n)
Next n
End Sub
' **************************************************************
' CHECKED OK !!
' Inserts s as new link, in place n and pushes forward existing
' links from n+1 to end.
'
Public Sub Insert_link(ByVal n As Long, ByRef Lst() As String, s As String)
Dim K As Long

If UBound(Lst) = 0 Then
Add_to_list Lst, s
Else ' Ubound(lst)>0
If n > 0 Then
If n <= UBound(Lst) Then ' Push and insert
ReDim Preserve Lst(UBound(Lst) + 1)
For K = UBound(Lst) - 1 To n Step -1
Lst(K + 1) = Lst(K)
Next K
Lst(n) = s ' assign new value to link n
Else ' n> ubound(lst)
Add_to_list Lst, s
End If ' If n<=0

Else ' n = 0
If n > 0 Then Add_to_list Lst, s
End If ' n>0

End If ' Ubound(lst)>0


End Sub
'==================================================================
' Added 4.2.2008
' Removes an item from array. CASE SENSITIVE !
' Empty string to be removed - in valid
' Empty arry is not an error, but is ignored
' Shifts back items after deletion.
Public Sub Remove_item_from_array(ByRef Lst() As String, ByVal s As String)
Dim Mx As Integer
Dim n As Integer
Dim K As Integer
Dim found As Boolean

Mx = UBound(Lst) ' Must be a non empty array


If Mx > 0 Then

If Mx = 1 And s = Lst(1) Then


ReDim Lst(0)
Else ' Array length >1
K = 0
found = False
Do
K = K + 1
found = Lst(K) = s
Loop Until found Or K = Mx

If found Then
If K < Mx Then
For n = K + 1 To Mx
Lst(n - 1) = Lst(n)
Next n
End If ' K<Mx
ReDim Preserve Lst(Mx - 1)
End If ' Found=true
End If ' len(lst)>0
End If 'Ubound(lst)>0 and len(s)>0
End Sub
'===============================================================

You might also like