Lotusscript:使用变量作为数组名称,因此可以在循环中应用函数

时间:2014-08-06 18:52:08

标签: lotus-notes lotusscript

我不确定我是否能很好地表达这个问题。这就是我想要完成的事情。

我的代理构建了一系列25个左右的数组。 Arr1()和Arr(2)等。它们是动态的,因为我不知道它们将包含多少项。

代理必须对每个数组应用多个函数。一个简单的是泡泡排序。

所以我有25行代码:

Call bub_sort(Arr1)
Call bub_sort(Arr2)
etc.

对于每个函数,我有25行代码(或类似的代码)。

随着时间的推移,我将添加额外的数组和可能不同的函数。

似乎最好加载数组名称的列表或数组,然后对函数进行迭代,如下所示

'Sort Arrays
ForAll lst In lstGrpNme
    Call bub_sort(lst)
End Forall 

但是,当我传递字符串而不是数组时,代码当然不起作用。

我该如何解决这个问题?

我担心我必须上课?

4 个答案:

答案 0 :(得分:4)

您可以创建所有数组的Variant数组,并使用Forall遍历所有数组:

Dim Array1(10) As String
Array1(0) = "one"
...
Dim Array2(10) As String
Array2(0) = "two"
...
Dim AllArrays(25) As Variant
AllArrays(0) = Array1
AllArrays(1) = Array2
...
Forall Array In AllArrays
    If Not Isempty(Array) Then
        If Ubound(Array) > 0 Then
            Print(Array(0))   ' prints every array's first element
            Call bub_sort(Array)
        End If
    End If
End Forall

如果您确定AllArrays的每个元素都已填充且每个数组至少有一个元素,则可以省略两个If行。

您也可以以类似的方式使用列表而不是数组。只需改变这一条昏暗线:

Dim AllArrays List As Variant

而不是数字0,1,2 ......,你可以给出数组名称,然后使用名称访问它们。

Const ARRAY_ONE = 0
Const ARRAY_TWO = 1
AllArrays(ARRAY_ONE) = Array1
AllArrays(ARRAY_TWO) = Array2
...
Dim TmpArray As Variant
TmpArray = AllArrays(ARRAY_ONE)
TmpArray(0) = "xxx"
AllArrays(ARRAY_ONE) = TmpArray

答案 1 :(得分:2)

这是一个类非常有用的典型例子。

  

我担心我必须上课?

不要害怕,课程其实很容易。我曾经写过两篇关于此的博客文章,如果您愿意,请看看: http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-1/ http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-2/

由于您认为以后可能会添加更多功能,因此应将其构建为类。 然后你可以做这样的事情:

Set myArray = New MyArrayClass(arr1,arr2)
Call myArray.bubbleSort

答案 2 :(得分:0)

如果您希望字面上将字符串作为数组名称传递,那么您可以使用Execute语句。为此,您必须将所有数组变量放入代理的(Declarations)部分 这是一个例子:

`Declarations
Dim tempArray As Variant 'This is temp array for performing operations
Dim array0 As Variant
Dim array1 As Variant
'... and so on

Sub Initialize

'...
    Dim arrayNames() As String

    Redim array0(2) As Integer
    Redim array1(1) As Integer

    array0(0) = 0
    array0(1) = 1
    array0(2) = 2

    array1(0) = 3
    array1(1) = 4

    Redim arrayNames(1) As String
    arrayNames(0) = "array0"
    arrayNames(1) = "array1"

    'Do something with all arrays
    Forall arrayName In arrayNames
        Call GetArray(arrayName)
        Call DoSomethingWithArray(tempArray)
        Call SetArray(arrayName)
    End Forall
'...

End Sub

'Put array to tempArray
Sub GetArray(arrayName As String)
    Execute "tempArray = " & arrayName$
End Sub

'Put tempArray to array
Sub SetArray(arrayName As String)
    Execute arrayName$ & " = tempArray"
End Sub

'Some function
Sub DoSomethingWithArray(array As Variant)
    For index% = 0 To Ubound(array)
        array(index%) = array(index%) + 1
    Next
End Sub

答案 3 :(得分:0)

我会使用一个数组列表和/或类似的类:

%REM
 Description: NOT TESTET! JUST FOR DEMONSTRATION!
%END REM
Public Class ArrayHandler
    Private ArrayList() As Variant
    Private ArrayListCounter As Long

    %REM
        Sub New
        Description: Constructor
    %END REM
    Public sub New(firstArray As Variant)
        ArrayListCounter = 0
        ReDim Preserve ArrayList(ArrayListCounter) As Variant
        ArrayList(ArrayListCounter) = firstArray
    End Sub
    %REM
        Sub addArray
        Description: self Explaining... ;)
    %END REM
    Public Sub addArray(nexArray As Variant)
        ArrayListCounter = ArrayListCounter + 1
        ReDim Preserve ArrayList(ArrayListCounter) As Variant
        ArrayList(ArrayListCounter) = nexArray
    End Sub

    %REM
        Sub bub_sort_allArrays
        Description: The Magic Function! ;-)
    %END REM
    Public Sub bub_sort_allArrays()
        ForAll arrs In ArrayList
            Call bub_sort(arrs)
        End ForAll
    End Sub

    %REM
        Sub bub_sort
        Description: YOUR FUNCTION!
    %END REM
    Public Sub bub_sort(Arr As Variant)
        '//-- YOUR CODE GOES HERE
    End Sub
End Class  

我希望这会对你有所帮助。 :)

编辑: 第二个更合适的想法,也许......:

Public Class ArrayHandler
Private ArrayList List As Variant

%REM
    Sub New
    Description: Constructor
%END REM
Public sub New(firstArray As Variant, firstArrayName As String)
    ArrayList(firstArrayName) = firstArray
End Sub
%REM
    Sub addArray
    Description: self Explaining... ;)
%END REM
Public Sub addArray(nexArray As Variant, nextArrayName As String)
    ArrayList(nextArrayName) = nexArray
End Sub

%REM
    Sub bub_sort_allArrays
    Description: The Magic Function! ;-)
%END REM
Public Sub bub_sort_allArrays()
    ForAll arrs In ArrayList
        Call bub_sort(ListTag(arrs))
    End ForAll
End Sub

%REM
    Sub bub_sort
    Description: YOUR FUNCTION!
%END REM
Public Sub bub_sort(Arr As Variant)
    '//-- YOUR CODE GOES HERE
End Sub

结束班