VBA - 使用多维数组而不是两个单独的数组?

发布于 2024-10-28 10:36:20 字数 1231 浏览 4 评论 0原文

最近有很多愚蠢的问题,但我希望能就此问题提供一些意见。 我有一个来自 INI 文件的字符串。它看起来像Firstname=FIRSTNAME。这基本上是一个包含这些负载的数组。我想把它们分开,但同时保留它们。因此,我设法将 Firstname 放入它自己的数组中,将 FIRSTNAME 放入它自己的数组中。但后来我的一位同事说“你为什么不直接使用多维数组呢?”。这让我开始思考,将 Firstname 放入 0,将 FIRSTNAME 放入 1。但是我该怎么做呢?

这是我现在的代码:

    For iTeller = 0 To UBound(arrIniName)
        If Not arrIniName(iTeller) = "" Then
            arrIniName(iTeller) = Split(arrIniName(iTeller), "=")(0)
        End If
    Next

    For iTeller = 0 To UBound(arrIniValue)
        If Not arrIniValue(iTeller) = "" Then
            arrIniValue(iTeller) = Split(arrIniValue(iTeller), "=")(1)
        End If
    Next

arrIniName 和 arrIniValues 都包含完全相同的数组。看起来像这样:

arrIniName(0) "Fistname=FIRSTNAME"
arrIniName(1) "Lastname=LASTNAME"
arrIniName(2) "Initials=INITIALS"

所以我基本上按照我现在的方式将每个数组分成自己的单独数组。但是将它们放入多维数组中可能会更好吗?因为这样我就只有一个数组需要管理,并且还可以通过 For Each 循环拉动该数组。

编辑:我最终这样做了,其中 Values 是数组

For Each s In Values
    Dim strName, strValue
    s = Split(s, "=")
    strName = s(0)
    strValue = s(1)

    'Run function strName, strValue
Next

Lots of stupid questions lately, but I would appreciate some input on this one.
I've got a string that comes from an INI-file. It looks like Firstname=FIRSTNAME. This is basically an array with loads of these. I want to split these up, but preserve both of them. So, I managed to put Firstname in it's own array and FIRSTNAME in it's own. But then a colleague of mine said "Why don't you just use a multidimensional array instead?". And this got me thinking, putting Firstname in 0 and FIRSTNAME in 1. But how do I do that?

This is my code right now:

    For iTeller = 0 To UBound(arrIniName)
        If Not arrIniName(iTeller) = "" Then
            arrIniName(iTeller) = Split(arrIniName(iTeller), "=")(0)
        End If
    Next

    For iTeller = 0 To UBound(arrIniValue)
        If Not arrIniValue(iTeller) = "" Then
            arrIniValue(iTeller) = Split(arrIniValue(iTeller), "=")(1)
        End If
    Next

Both arrIniName and arrIniValues consists of the exact same array to begin with. Which looks like this:

arrIniName(0) "Fistname=FIRSTNAME"
arrIniName(1) "Lastname=LASTNAME"
arrIniName(2) "Initials=INITIALS"

So I basically split each one into their own separate arrays the way I do it now. But putting them in a multi dimensional array would probably be better? Because then I'd have just one array to manage and could also pull that array through a For Each loop.

Edit: I ended up doing it like this, where Values is the array

For Each s In Values
    Dim strName, strValue
    s = Split(s, "=")
    strName = s(0)
    strValue = s(1)

    'Run function strName, strValue
Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-11-04 10:36:20

理想的解决方案听起来像一个 字典 (一种保存键/值对的数据结构 - 确切地说INI 文件中的内容)。

这里不需要多维数组,因为只有 2 个维度(键和值)。数组通常比字典更难使用,因为它们很难调整大小,因此您需要预先知道有多少项。

因此,我建议使用以下代码:

Dim dict As Dictionary
Set dict = new Dictionary
Dim key as String
Dim value as String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        dict.Add(key, value)
    End If
Next

但是,如果您想使用多维数组,则可以使用以下代码:

' Declare a 2-dimensional array, of dimensions "n by 2".
Dim results(UBound(arrIniValue), 2) As String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        results(iTeller, 0) = key
        results(iTeller, 1) = value
    End If
Next

The ideal solution sounds like a Dictionary (a data structure which holds Key/Value pairs - exactly what you have in your INI file).

A multi-dimensional array would not be necessary here, as you only have 2 dimensions (key, and value). Arrays are generally more difficult to work with than dictionary's as they are hard to resize, so you need to know how many items you have upfront.

Therefore, I would suggest the following code:

Dim dict As Dictionary
Set dict = new Dictionary
Dim key as String
Dim value as String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        dict.Add(key, value)
    End If
Next

However, if you want to use a multi-dimensional array, then the following will do it:

' Declare a 2-dimensional array, of dimensions "n by 2".
Dim results(UBound(arrIniValue), 2) As String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        results(iTeller, 0) = key
        results(iTeller, 1) = value
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文