Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: beginner
Dec 23, 2000

ArrShortestItem - The value and index of the shortest element of an array of any type

' The shortest item in an array of any type
' (it applies the LEN function to all the items in the array
'  and then takes the highest value)
'
' FIRST and LAST indicate which portion of the array
' should be considered; they default to the first
' and last element, respectively
' if SHORTESTINDEX is specified, it receives the index of
' the longest element of the array

Function ArrShortestItem(arr As Variant, Optional ByVal First As Variant, _
    Optional ByVal Last As Variant, Optional ShortestItem As Long) As Variant
    Dim index As Long
    Dim minLength As Long
    
    If IsMissing(First) Then First = LBound(arr)
    If IsMissing(Last) Then Last = UBound(arr)
    
    ArrShortestItem = arr(First)
    minLength = Len(ArrShortestItem)
    ShortestItem = First
    
    For index = First + 1 To Last
        If minLength > Len(arr(index)) Then
            ArrShortestItem = arr(index)
            minLength = Len(ArrShortestItem)
            ShortestItem = index
        End If
    Next
    
End Function

Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date