Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Comparing to Arrays

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Comparing to Arrays

unless the arrays are sorted, I would think you would have to loop and
compare on each element.

If they were on a worksheet, you could use the countif formula.

--
Regards,
Tom Ogilvy

"Marston" wrote in message
om...
I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Comparing to Arrays

Marston wrote:

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?


What does "forever" mean? Approximately how long does it take?

Alan Beban
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Comparing to Arrays

Marston wrote:

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?


And what version of Excel are you using?

Alan Beban
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Comparing to Arrays

I don't know if this will be faster than looping through the 2nd array, but
you can try it:

Dim Found As Variant
For i = 1 to UBound(Array1)
Found=(Application.Match(Array1(i),Array2,0))
If IsError(Found) Then
'not there
Else
'a match
End If
Next i



On 31 Aug 2004 07:34:32 -0700, (Marston) wrote:

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Comparing to Arrays

Sub Tester9()
Dim myarray(1 To 5462)
For i = 1 To UBound(myarray)
myarray(i) = i
Next
res = Application.Match(Int(Rnd() * UBound(myarray)), myarray, 0)
If Not IsError(res) Then
MsgBox res
End If
End Sub

raises a type mismatch error. Match doesn't work with an array with more
than 5461 elements, at least not in xl2000 and below. One reason I didn't
suggest that.

It may be more robust in Excel 2002/3.

--
Regards,
Tom Ogilvy

"Myrna Larson" wrote in message
...
I don't know if this will be faster than looping through the 2nd array,

but
you can try it:

Dim Found As Variant
For i = 1 to UBound(Array1)
Found=(Application.Match(Array1(i),Array2,0))
If IsError(Found) Then
'not there
Else
'a match
End If
Next i



On 31 Aug 2004 07:34:32 -0700, (Marston) wrote:

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Comparing to Arrays

Hi, Tom:

I'm using XL 2002 (XP) and your code works fine there. I also tried it with an
array Dim'd 1 to 33,000 and there no problems. It's nice to see that MS has
actually fixed some bugs.

Thanks for the caveat for XL97 and 2000 users.

BTW, in some testing I did with either XL5 or 95, MATCH on a sorted array was
slower than a binary search, but that changed in a later version.

Myrna Larson


On Tue, 31 Aug 2004 14:22:08 -0400, "Tom Ogilvy" wrote:

Sub Tester9()
Dim myarray(1 To 5462)
For i = 1 To UBound(myarray)
myarray(i) = i
Next
res = Application.Match(Int(Rnd() * UBound(myarray)), myarray, 0)
If Not IsError(res) Then
MsgBox res
End If
End Sub

raises a type mismatch error. Match doesn't work with an array with more
than 5461 elements, at least not in xl2000 and below. One reason I didn't
suggest that.

It may be more robust in Excel 2002/3.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Comparing to Arrays

BTW, the fastest way to search is a binary search, but it requires that the
array being searched is sorted in ascending order. Is your data sorted? If
not, can you sort it?

I can give you routines for sorting and for a binary search if this approach
is feasible.

On 31 Aug 2004 07:34:32 -0700, (Marston) wrote:

I have two relatively large 1D arrays (I could make them into a two D
with a dummy column if that would help, but I can't imagine how). Both
arrays contain a series of strings, with each string being exactly the
same length (in this case 18 characters). The characters are numbers
as strings. Each array contains 20,000-30,000 elements.

The challenge is finding a fast way to compare each element in one
array to see if it exists in the second array. I find that if I do
this through any simple stepwise looping it takes forever. Any
suggestions?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing ranges/arrays asaylor Excel Worksheet Functions 7 June 13th 06 11:53 PM
Comparing 2 arrays [email protected] Excel Worksheet Functions 3 January 2nd 06 02:31 AM
Comparing Arrays TangentMemory Excel Discussion (Misc queries) 2 May 13th 05 05:06 PM
Comparing Arrays KL Excel Worksheet Functions 9 December 3rd 04 08:58 PM
Comparing two arrays/ranges SpiderBoy Excel Programming 0 July 25th 03 07:58 PM


All times are GMT +1. The time now is 11:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"