匹配字符串数组值 [英] Matching values in string array

查看:111
本文介绍了匹配字符串数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:寻找寻找是否有一维数组精确匹配值的更有效的方式 - 基本上是一个布尔值真/假

我俯瞰明显的东西吗或者,我只是使用了错误的数据结构,使用的时候我也许应该使用一个集合对象或字典一个数组?在后者,我可以检查。载 .Exists 方法,分别为

在Excel中,我可以在一个矢量数组就像检查一个值:

 如果不ISERROR(Application.Match(strSearch,varToSearch,假))然后
' 做东西
万一

这将返回精确匹配指数,显然受到匹配功能的局限性,只有在这种情况下发现的第一个的匹配值。这是一种常用的方法,另一个我已经使用了很长一段时间了。

这足以满足为Excel - 但对于其他应用程序

在其它应用中,我所能做的基本上是相同的事情,但需要启用参考Excel对象库,然后:

 如果不ISERROR(Excel.Application.match(...))

这似乎是愚蠢的,虽然,很难管理,因为权限/信任中心/等。

分布式文件

我曾尝试使用过滤器()功能:

 如果不UBOUND(滤镜(varToSearch,strSearch))= -1,那么
    '做东西
 万一

不过,这种方法的问题是,过滤器返回部分匹配的数组,而不是完全匹配的数组。 (我不知道为什么它会返回子/部分匹配是有用的。)

另一种方法是在阵列中的每个值字面上迭代(这也是很常用的,我认为) - 这似乎更是不必要的繁琐比呼吁Excel的匹配功能。

 对于VARRAY各V
   如果v = strSearch然后
    ' 做东西
   万一
下一个


解决方案

如果我们要谈论的表现则没有substutute运行一些测试。根据我的经验Application.Match()比调用它使用一个循环功能要慢十倍。

 子仪()    昏暗我一样长,B,T
    昏暗的ARR(1〜100)作为字符串    对于i = 1到100
        ARR(ⅰ)=VALUE_与&一世
    接下来,我    T =定时器
    对于i = 1到100000
        B =包含(ARRValue_50)
    接下来,我
    Debug.Print包含,定时器 - T的    T =定时器
    对于i = 1到100000
        B = Application.Match(ARR,Value_50,FALSE)
    接下来,我
    Debug.Print匹配,定时器 - T的结束小组
函数包含(ARR,V)为布尔
昏暗RV作为布尔,磅长,UB长,我只要
    磅= LBOUND(ARR)
    UB = UBound函数(ARR)
    对于i =磅为UB
        如果ARR(I)= V,那么
            RV = TRUE
            对于出口
        万一
    接下来,我
    包含= RV
结束功能

Problem: Looking for a more efficient way of finding whether there is an exact matching value in a 1d array -- essentially a boolean true/false.

Am I overlooking something obvious? Or am I simply using the wrong data structure, by using an array when I probably should be using a collection object or a dictionary? In the latter I could check the .Contains or .Exists method, respectively

In Excel I can check for a value in a vector array like:

If Not IsError(Application.Match(strSearch, varToSearch, False)) Then
' Do stuff
End If

This returns an exact match index, obviously subject to limitations of Match function which only finds the first matching value in this context. This is a commonly used method, and one that I have been using for a long time, too.

This is satisfactory enough for Excel -- but what about other applications?

In other applications, I can do basically the same thing but requires enabling reference to the Excel object library, and then:

   If Not IsError(Excel.Application.match(...))

That seems silly, though, and is difficult to manage on distributed files because of permissions/trust center/etc.

I have tried to use the Filter() function:

 If Not Ubound(Filter(varToSearch, strSearch)) = -1 Then
    'do stuff
 End If

But the problem with this approach is that Filter returns an array of partial matches, rather than an array of exact matches. (I have no idea why it would be useful to return substring/partial matches.)

The other alternative is to literally iterate over each value in the array (this also is very commonly used I think) -- which seems even more needlessly cumbersome than calling on Excel's Match function.

For each v in vArray
   If v = strSearch Then
    ' do stuff
   End If
Next

解决方案

If we're going to talk about performance then there's no substutute for running some tests. In my experience Application.Match() is up to ten times slower than calling a function which uses a loop.

Sub Tester()

    Dim i As Long, b, t
    Dim arr(1 To 100) As String

    For i = 1 To 100
        arr(i) = "Value_" & i
    Next i

    t = Timer
    For i = 1 To 100000
        b = Contains(arr, "Value_50")
    Next i
    Debug.Print "Contains", Timer - t

    t = Timer
    For i = 1 To 100000
        b = Application.Match(arr, "Value_50", False)
    Next i
    Debug.Print "Match", Timer - t

End Sub


Function Contains(arr, v) As Boolean
Dim rv As Boolean, lb As Long, ub As Long, i As Long
    lb = LBound(arr)
    ub = UBound(arr)
    For i = lb To ub
        If arr(i) = v Then
            rv = True
            Exit For
        End If
    Next i
    Contains = rv
End Function

这篇关于匹配字符串数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆