Compare & Filter Strings/Arrays

# Thread: Compare & Filter Strings/Arrays

1. Mhat Guest

## Compare & Filter Strings/Arrays

On a page I&#039;m working on, I have 4 dynamically-created strings I&#039;d like to compare...each string is just a comma-separated list of up to 20 different numbers. What I want to do is to find all numbers that appear in more than one string, and then make a new array with just those numbers, sorting by numbers which appeared the greatest number of times first. I&#039;m way too retarded to figure this out, can anyone help? mhat@samplehead.com

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Compare & Filter Strings/Arrays

There are so many ways to do this that I almost hesitate to suggest any one way lest you decide it is the only one!<BR><BR>Do you have any foreknowledge about the *range* of numbers that will be found in the lists? That is, do you know that they will only be integers from 1 to 1000 or something like that? If your answer to that is "yes," then one way would be to simply create an array as big as the expected range and then "count" things by bumping the cell that corresponds to the number(s):<BR><BR>&#060;%<BR>Dim counts(1000)<BR><BR>Sub processString( str )<BR>&nbsp; &nbsp; dim temp, i, n<BR>&nbsp; &nbsp; temp = Split(str,",")<BR>&nbsp; &nbsp; For i = 0 To UBound( temp )<BR>&nbsp; &nbsp; &nbsp; &nbsp; n = CInt( temp( i ) )<BR>&nbsp; &nbsp; &nbsp; &nbsp; counts( n ) = counts( n ) + 1<BR>&nbsp; &nbsp; Next<BR>End Sub<BR>%&#062;<BR><BR>Then, after processing all the strings through that SUB, you simply look through the "counts" array for all elements that have a count greater than 1 as well as looking for the greatest of those.<BR><BR>**************<BR><BR>But let&#039;s assume that you have no idea what the range of numbers will be, and maybe you have numbers such as 0.33333333333 and 3.14159265.<BR><BR>Then the best way is probably to convert each string to an array and do a "merge/sort". This is hairy enough that I won&#039;t go into it unless you need it.<BR><BR>

3. Mhat Guest

## RE: Compare & Filter Strings/Arrays

I don&#039;t see how this works exactly... <BR>If I had 4 strings:<BR>"1, 2, 3, 4"<BR>"1, 4, 6, 7"<BR>"1, 2, 9, 11"<BR>"2, 4, 9, 11"<BR>I&#039;d want the results to list:<BR>"1, 2, 4, 9, 11, 3, 6, 7", i.e. list the numbers that occurred the most times out of all 4 strings first.<BR>Would the code you wrote do this? I don&#039;t see how really... Please help if you can, I do appreciate it, and if you could provide a means of contacting you directly, that would be most appreciated.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•