Recent Post

QTP - Array Library Functions


QTP & ARRAY LIBRARY FUNCTIONS

'*******************************************************
'*********** ARRAY RELATED LIBRARY FUNCTIONS ***********'
'*******************************************************
Print "========================================="
Print "========================================="
Dim ArrayToSort(4)
ArrayToSort(0)="1234"
ArrayToSort(1) ="4567"
ArrayToSort(2)="3"
ArrayToSort(3)="1"
ArrayToSort(4)="55"

Print "========================================="
Print "Before Sorting out -->Array Values are: "
Print "========================================="
For k=0 to UBound(ArrayToSort)
Print  ArrayToSort(k)
Next
Print "========================================="
Print " Sort Order Function Called to sort Array Values : "
Print " Sort Order Mode is : dsc (DSCENDING order)"
Print "========================================="
Call SortODArray(ArrayToSort,"dsc")

Print "========================================="
Print "After Sorting out --> Array Values are  : "
Print "========================================="
For k=0 to UBound(ArrayToSort)
Print  ArrayToSort(k)
Next

'********************************************************
' Author    : G A Reddy
' Function  : SortODArray
' To sort One Dimensional Array in ascending OR dscending order
' PARAMETERS : ArrayToSort (Array to be passed)
' SortMode asc (ASCENDING) OR des(FOR DESCENDING)
'********************************************************

Function SortODArray(ArrayToSort,SortMode)
  On Error Resume Next   ' Error Handling
  
    Dim iResult, i,j
    Dim iUbound
    Dim sTemp
                Dim  SortedArray()
  
    SortMode = UCase(SortMode)

    iUbound = UBound(ArrayToSort)
    Redim SortedArray(iUbound)

    For i = 0 To iUbound - 1
      For j = 0 To (iUbound - i-1)
      If Instr(SortMode, "ASC") > 0 then
      If CompareArrayValues(ArrayToSort(j + 1), ArrayToSort(j)) < 0 Then
                    sTemp = ArrayToSort(j)
                    ArrayToSort(j) = ArrayToSort(j + 1)
                    ArrayToSort(j + 1) = sTemp
                End If
            Else
       If CompareArrayValues(ArrayToSort(j + 1), ArrayToSort(j)) > 0 Then
                    sTemp = ArrayToSort(j)
                    ArrayToSort(j) = ArrayToSort(j + 1)
                    ArrayToSort(j + 1) = sTemp
                End If
            End If
        Next
    Next
                SortODArray = ArrayToSort   
                ' ArrayToSort - Values are sorted now "

             If Err.Number > 0 then
            msgbox CStr(err.number) & " " &  Err.Description
     End If

End Function
'******************************************************
'******************************************************
' Author      :  G A Reddy
' Function   :  CompareArrayValues
' Note : Array can have DATA TYPES - DATE, NUMERIC OR TEXT/STRING
' So we find out data type in array and compare values to sortout
' Return Values : Returns "-1" if val1 < val2, returns "1" if val1 > val2
'   RETURN "0" if val1 = val2
'*******************************************************
Function CompareArrayValues(Val1, Val2)
   On Error Resume Next  
    Dim FVal,SVal, RVal
    ' FVal= First Value ; SVal=Second Value ; RVal = Return Value
               
    if (isNumeric(Trim(Val1)) AND isNumeric(Trim(Val2))) then
        FVal = CDbl(Trim(Val1))
        SVal = CDbl(Trim(Val2))
    else
        if (isDate(Trim(Val1)) AND isDate(Trim(Val2))) then
            FVal = CDate(Trim(Val1))
            SVal = CDate(Trim(Val2))
        else
            FVal =  Trim(CSTR(Val1))
            SVal = Trim(CSTR(Val2))
        end if
    end if
   
    RVal=0
        If FVal < SVal then
            RVal = -1
        else
            if FVal > SVal then
                RVal = 1
            end if
        end if
CompareArrayValues = RVal

              If Err.Number > 0 then
              Msgbox CStr(err.number) & " " &  Err.Description
      End If

End Function
'******************************************************
'******************************************************

0 comments:

Post a Comment

GAReddy @ OneTestingCenter @ All Articles