VBSCript For Begineers(Array)

Author Message
amitkumar.s

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 5/3/2007
  • Status: offline
VBSCript For Begineers(Array) Thursday, May 03, 2007 5:54 PM (permalink)
0
Public Function CompareArrays(ExpArray, ActArray, Test_step)
 
 iResult=True
 If (UBound(ExpArray)<>UBound(ActArray)) Then
 call Logger(Test_step,"Array comparision failed as the Sizes of Arrays are not equal","Fail")
 Else
 array_size=UBound(ExpArray)
 End If
 For i=0 to array_size
  
  If (LCase(Trim(ExpArray(i)))<>LCase(Trim(ActArray(i))))Then
   iResult=False
  Else
   iResult=True
  End If
  Next
  
 End Function
 
 Public Function CompareinArray(ExpArray, ActArray, Test_step)
 
 array_size=UBound(ExpArray)
 
 For i=0 to array_size
  iResult=False
  For j=0 to UBound(ActArray)
   If (LCase(Trim(ExpArray(i)))=LCase(Trim(ActArray(j))))Then
   iResult=True
   Exit For
  End If
  Next
  
  If NOT iResult Then
   iResult=False
  End If
 Next
 
 End Function
 
 
 
 Public Function CompareValueinArray(StringVal, ExpArray, Test_step)
 
 iResult=False
 
 array_size=UBound(ExpArray)
 
 For i=0 to array_size
   If (LCase(Trim(ExpArray(i)))=LCase(Trim(StringVal)))Then
    iResult=True
   End If
 Next
  If  iResult Then
   CompareValueinArray=True
  Else
   CompareValueinArray=False
  End If
 End Function
 
 
 
 Public Function Join_Arrays(Array1, Array2, Joined_Array)
  ReDim Joined_Array(Ubound(Array1)+Ubound(Array2)+1)
  
  For ind=0 to Ubound(Array1)
   Joined_Array(ind)=Array1(ind)
  Next
  
  For ind=0 to Ubound(Array2)
   Joined_Array(Ubound(Array1)+1+ind)=Array2(ind)
  Next
 
 End Function
 
 
 
 Public Function ArraySort_desc(arrShort)
    For i = UBound(arrShort) - 1 To 0 Step -1
    For j= 0 to i
    If arrShort(j) < arrShort(j+1) then
    temp=arrShort(j+1)
    arrShort(j+1)=arrShort(j)
    arrShort(j)=temp
    end if
    next
    Next
 End Function
 
 
 
 Public Function ArraySort_asc(arrShort)
    For i = UBound(arrShort) - 1 To 0 Step -1
    for j= 0 to i
    if arrShort(j) > arrShort(j+1) then
    temp=arrShort(j+1)
    arrShort(j+1)=arrShort(j)
    arrShort(j)=temp
    end if
    next
    Next
 End Function
 
 
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    RE: VBSCript For Begineers(Array) Friday, May 04, 2007 2:55 AM (permalink)
    0
    Thanks for sharing your code. Some comments:
     
    Public Function CompareArrays(ExpArray, ActArray, Test_step)
      iResult=True
      If (UBound(ExpArray)<>UBound(ActArray)) Then
        call Logger(Test_step,"Array comparision failed as the Sizes of Arrays are not equal","Fail")
      Else
        array_size=UBound(ExpArray)
      End If
      For i=0 to array_size
     
         If (LCase(Trim(ExpArray(i)))<>LCase(Trim(ActArray(i))))Then
            iResult=False
         Else
            iResult=True
         End If
       Next
     
    End Function
     
    iResult=True is misleading. You name the variable with a name that indiocates that it will be an integer yet you use it like a boolean.
    call Logger(Test_step,"Array comparision failed as the Sizes of Arrays are not equal","Fail") - There is no Logger function provided
    The logic of the function is faulty. The way it is written, it will return true anytime that the last elements of the array are equal and the arrays are the same size regardless of whether all the rest of the elements are equal.
    The Test_Step parameter is never used so there really isn't any point to passing it into the function.
    The function never returns a result.
     
     
     
    Public Function CompareinArray(ExpArray, ActArray, Test_step)
    array_size=UBound(ExpArray)
    For i=0 to array_size
     iResult=False
     For j=0 to UBound(ActArray)
      If (LCase(Trim(ExpArray(i)))=LCase(Trim(ActArray(j))))Then
      iResult=True
      Exit For
     End If
     Next
     
     If Not iResult Then
      iResult=False
     End If
    Next
    End Function
     
     
    If you move iResult=False out of the For loop at the beginning then there is no need for the If Not iResult Then logic block any more.
    The Test_Step parameter is never used so there really isn't any point to passing it into the function.
    This function doesn't actually return any result.
     
     
    Public Function CompareValueinArray(StringVal, ExpArray, Test_step)
    iResult=False
    array_size=UBound(ExpArray)
    For i=0 to array_size
      If (LCase(Trim(ExpArray(i)))=LCase(Trim(StringVal)))Then
       iResult=True
      End If
    Next
     If  iResult Then
      CompareValueinArray=True
     Else
      CompareValueinArray=False
     End If
    End Function
     
    The Test_Step parameter is never used so there isn't really any point to passing it into the function.
    The If iResult Then block is pointless. All you need to do is CompareValueInArray = iResult.
     
    Neither of the sort functions actually sort the array.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      anrmurthy

      • Total Posts : 2
      • Scores: 0
      • Reward points : 0
      • Joined: 5/1/2007
      • Status: offline
      RE: VBSCript For Begineers(Array) Tuesday, May 22, 2007 12:03 AM (permalink)
      0
      Hi Amit

      This is Murthy, i am new to this VB Script could you help me in writting the VB code  for sorting as i have tried a lot but i haven't got if you could please try for me and post it to my mail id: PLEASE DO NOT FEED THE SPAMMERS
      Regards
      Murthy.
       
      EDIT (EBGREEN): Removed email address. Use the Private Messaging system to contact a person.
      <message edited by ebgreen on Tuesday, May 22, 2007 1:49 AM>
       
      #3
        kvkanth008

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 11/1/2009
        • Location: Hyderabad
        • Status: offline
        Re: RE: VBSCript For Begineers(Array) Sunday, November 01, 2009 6:31 PM (permalink)
        0
        Hi,

        I've query on Alphabetical Sorting...

        How can I verify that all the items in the Drop Down are in Alphabetical Order., Here, I want to test that whether the items in the drop down are in alphabetical order or not. if the Items are in alphabetical order it should return True else it should return False

        Please do the needful....

        Thanks in Advance.

        Regards,
        VarunK
         
        #4
          ehvbs

          • Total Posts : 3321
          • Scores: 110
          • Reward points : 0
          • Joined: 6/22/2005
          • Location: Germany
          • Status: offline
          Re: RE: VBSCript For Begineers(Array) Tuesday, November 03, 2009 12:35 AM (permalink)
          0
          For simple arrays, you may get away with

             Dim aTests : aTests = Array( _
                Array( 1, 2, 4, 5 ), True _
              , Array( "a", "d", "z" ), True _
              , Array( "z", "d", "e" ), False _
              , Array( 1, 2, 1 ), False _
             )
             Dim nTest
             For nTest = 0 To UBound( aTests ) Step 2
                 Dim bRes : bRes = isSorted( aTests( nTest ) )
                 WScript.Echo Join( aTests( nTest ) ), CStr( bRes ), CStr( bRes = aTests( nTest + 1 ) )
             Next
           
           Function isSorted( aX )
             ' test if simple array aX (one dimension, all items comparable with >=) is sorted
             Dim nIdx
             For nIdx = 1 To UBound( aX )
                 If aX( nIdx - 1 ) > aX( nIdx ) Then
                    isSorted = False
                    Exit Function
                 End If
             Next
             isSorted = True
           End Function
           


          output
           === TestIsSorted: test if simple array is sorted ==
           1 2 4 5 Wahr Wahr
           a d z Wahr Wahr
           z d e Falsch Wahr
           1 2 1 Falsch Wahr
           === TestIsSorted: 0 done (00:00:00) ===============
           



           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9