Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Length of Array Items

Status
Not open for further replies.

hinchdog

Programmer
Feb 14, 2001
380
US
I was wondering what would be the fastest way to compare the length of various string elements stored in an array. i.e.

I had an array of 3 strings, X,Y and Z

len(X) = 5
len(Y) = 15
len(z) = 2

I want to retrieve the longest value (in this case 15). what i the quickest way to do this using VB 6?

thanks,
frank
 
? Qickest way to the longest string??

You may few a 'few' opinions on this!

Since you indicate the array of strings already exists, I will not go into the various methods of breaking down a "sentence" into words.

I know of no way to get the length of all of array elemenys without itterating through the collection. e.g. just do the loopy thing. So the only issue is to find the longest one in the min time. I think this has to be to just compare the current to the (to date) largest and replace. You can do this in the same loop where you get the length of each element:

For Idx = 0 to UBound(MyAry)
[tab]If (Len(MyAry(Idx)) > MaxLen)) then
[tab][tab]MaxLen = Len(MyAry(Idx)
[tab]'You MAY want to also save either the actual string
[tab]'or the index value here also.
[tab]End If
NextIdx





MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
i will try that out. thanks for your help!

-frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top