Function To Replace Array Element in LotusScript?

I want a function that would allow me to replace any “array type” value for another. For example:

v(0) = “A”

v(1) = “B”

v(2) = “C”

call fReplace(v, 1, “x”)

’ would return

v(0) = “A”

v(1) = “x”

v(2) = “C”

as i couldn’t find anything in LotusScript, i tried to create mine:

Function fReplace(v, posicion As Integer, replacement) As Variant

Dim nv () As Variant



For i = Lbound(v) To Ubound(v)

	Redim Preserve nv(i)

	If i = posicion Then

		nv(i) = v(i)

	Else

		nv(i) = replacement

	End If

Next



fReplace = nv

end function

but i get a type mismatch on the last fReplace = nv

any idea? Thanks.

Subject: Function To Replace Array Element in LotusScript?

Sub Initialize Dim v

v = Split("A,B,C", ",")

Call fReplace(v, 1, "x")	

End Sub

Sub fReplace(v, posicion As Integer, replacement)

v(posicion) = replacement

End Sub

Subject: Function To Replace Array Element in LotusScript?

Hi Luis,

I ran your code but did not get a type mismatch…

Three things though:

  1. You call the function fReplace, but you don’t store the result in a variable in the calling routine. So the values of the array v won’t be changed there

  2. The result of the function will be nv(0) = “x”, nv(1) =“B”, nv(3)="x’. You should change i = posicion to i <> posicion

  3. Why not use v(1) =“x” in your main routine? There is no specific need to write a function for it, since you don’t change the length of the array. You can just assing the new value to the array element.

Regards,

René

Subject: RE: Function To Replace Array Element in LotusScript?

thanks, actually i want to use this to change an element of a multi values field.

Subject: Function To Replace Array Element in LotusScript?

do not know if this works but I had it stored in my code db

Public Function RemoveEntries( ValuesToRemove, arrayToRemoveFrom, compMethod As Integer ) As Variant

'=== Removes entries from arrayToRemoveFrom whos value is in the ValuesToRemove array

'=== Returns a variant that contains an array value

'=== ValuesToRemove and arrayToRemoveFrom can be either a Variant that contains an array

'=== or an Array ( this is the reason there is no data type defined )

'=== compMethod is used as defined in the StrCompare function;

’ A number designating the comparison method. Use 0 for case-sensitive and pitch-sensitive,

’ 1 for case-insensitive and pitch-sensitive, 4 for case-sensitive and pitch-insensitive,

’ 5 for case-insensitive and pitch-insensitive. Use 2 to specify string comparison in

’ the platform’s collation sequence. If 2 is specified, strings are compared bit-wise.

’ … see help for details

If Not Isarray( arrayToRemoveFrom ) Then Exit Function

'— Store this return value in a variant to it can be converted into an array

Dim IndexList As Variant



Dim newList As Variant

Dim i As Integer

Dim checkList List As Integer

Dim ctr As Long

Dim lowVal As Integer, highVal As Integer

Dim numToRemove As Integer

Dim foundFlag As Integer



lowVal = Lbound( arrayToRemoveFrom )

highVal = Ubound( arrayToRemoveFrom )

'— Initialize array, we are about to use Redim preserve to incrment array size

Redim newList( 0 )

'— Loop through entries

ctr = 0

For i = lowVal To highVal

'— If this index position is not in the checkList then

     foundFlag = False

'— Loop through vals to remove and if there is a match flag it

     Forall aValueToRemove In ValuesToRemove

          If Strcompare( aValueToRemove, arrayToRemoveFrom( i ), compMethod ) = 0 Then

               foundFlag = True

               Exit Forall

          End If

     End Forall

'— If not found in list of entries to remove, then add to new array

     If Not foundFlag Then

'— Increment array, preserving existing value

          Redim Preserve newList( ctr )

'— Set value to current value

          newList( ctr ) = arrayToRemoveFrom( i )

'— increment counter for next time 'round

          ctr = ctr + 1

     End If

Next

'— Return value

RemoveEntries = newList

End Function

Subject: RE: Function To Replace Array Element in LotusScript?

simple example

Sub Click(Source As Button)

Dim v As Variant

'generate values in "v"

Redim v(2)

v(0) = "A"

v(1) = "B"

v(2) = "C"



' call change value in "v"

v = fReplace(v, 1, "x")

End Sub

Function fReplace( v As Variant, posicion As Integer, replacement As String) As Variant

v(posicion) = replacement

fReplace = v

End Function