Lotusscript Replace Function - Wrong Number of Array Subscripts

I seem to have come across a bit of a bug with the Replace function in Lotusscript. I’m taking a full list of values, finding a subset of those values and replacing them with blanks, i.e. taking [1][2][3][a][b][c] and ending up with [a][b][c]

If I do it like this, it works fine:

Sub Click(Source As Button)

Dim a1() As String

Redim a1(0 To 5) As String

a1(0) = "1"

a1(1) = "2"

a1(2) = "3"

a1(3) = "a"

a1(4) = "b"

a1(5) = "c"



Dim a2() As String

Redim a2 (0 To 2) As String

a2(0) = "1"

a2(1) = "2"

a2(2) = "3"



Dim a3

a3 = Replace(a1, a2, "")

End Sub

however, if I handle the population of my arrays a1 and a2 in subroutines like this:

Sub SetA1(arr() As String)

Redim arr(0 To 5) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"

arr(3) = "a"

arr(4) = "b"

arr(5) = "c"

End Sub

Sub SetA2(arr() As String)

Redim arr (0 To 2) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"

End Sub

Sub Click(Source As Button)

Dim a1() As String

Call SetA1(a1)	



Dim a2() As String

Call SetA2(a2)





Dim a3

a3 = Replace(a1, a2, "")

End Sub

I get the error ‘Wrong number of array subscripts’ on the a3=Replace… line, even though according to the debugger, all of my variables are exactly the same as they were in the first example.

I’ve been going round and round in circles with this passing variables to the subroutines different ways, declaring the arrays as strings or variants, redimming them as strings or variants, but can’t find a way that Replace will work when a1 and a2 have been modified in subroutines - I always either end up with the wrong number of subscripts error, or a Type Mismatch error.

I can’t declare the arrays as static arrays because in ‘real life’ i don’t know how big the arrays will be before I start, and I want to be able to use subroutines to avoid repetition of code as part of a much larger function but there doesn’t seem to be any way of doing it.

I’ve searched the forum and IBM support and found similar issues (i’ve fallen foul of Notes crashing when passing dynamic arrays to the Replace function before), but nothing describing this particular scenario.

I’m now going to resort to a workaround of creating my own replace function, or evaluating @Replace instead, but wanted to post this to see if anyone else had come across this or had any ideas about it.

Emily.

Subject: Lotusscript Replace Function - Wrong Number of Array Subscripts

I modified a little Your code:

Function SetA1() As Variant

Redim arr(0 To 5) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"

arr(3) = "a"

arr(4) = "b"

arr(5) = "c"



SetA1 = arr()

End Function

Function SetA2() As Variant

Redim arr (0 To 2) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"



SetA2 = arr()

End Function

Sub Click(Source As Button)

Dim a3

a3 = Replace(SetA1, SetA2, "")

End Sub

…and it works. Maybe that will help You.

Konrad

Subject: RE: Lotusscript Replace Function - Wrong Number of Array Subscripts

Konrad - thanks so much for responding. Interesting that it works that way, however what if I want to use a1 and a2 for other things as well? I don’t really want to have to call SetA1 and SetA2 every time I want to refer to that particular set of data. Is there any way that this can work declaring a1 and a2 in my main function but modifying them in subroutines?

Emily.

Subject: RE: Lotusscript Replace Function - Wrong Number of Array Subscripts

Got it! You have to declare the arrays as initialised dynamic arrays rather than uninitialised dynamic arrays. It will then work. Here is the sample code:

Sub Click(Source As Button)

Redim a1(0) As String

Call SetA1(a1)	



Redim a2(0) As String

Call SetA2(a2)



Dim a3

a3 = Replace(a1, a2, "")

End Sub

Sub SetA1(arr() As String)

Redim arr(0 To 5) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"

arr(3) = "a"

arr(4) = "b"

arr(5) = "c"

End Sub

Sub SetA2(arr() As String)

Redim arr (0 To 2) As String

arr(0) = "1"

arr(1) = "2"

arr(2) = "3"

End Sub

Thanks again Konrad!