Help with arrays

Hello.

I am working on a piece of code that is part of a larger script that grabs data from a Notes Db, manipulates it and sends it to a disparate system. There is a new requirement that has me stumped, and hopefully someone can provide assistance.

I have two separate text fields (arrays) whose values are separated by the “|” symbol. For my code, the fields are dependent on each other for data later in the code. The elements in each array are always the same, meaning if field1 has five elements in the array, field2 always has five elements as well.

The issue is field1 can have duplicate entries, and I’m having a problem matching the duplicate entries (which should only count as 1 entry) with the corresponding entries in field2.

An example of my code is here:

dim sess as new notessession

dim db as notesdatabase

dim doc as notesdocument

dim fldcombo list as string

c1cnt =0

if doc.hasitem(“field1”) then

c1 = doc.field1(0)

if c1 <> “” then

eformulaC = |@explode(field1; “||”)|

evalC = evaluate(eformulaC, doc)

eformulaS = |@explode(field2; “||”)|

evalS = evaluate(eformulaS, doc)

forall y in evalC

if y <> “” then

x = evalS(c1cnt)

fldcombo(c1cnt) = y & “|” & x

msgbox fldcombo

c1cnt = c1cnt + 1

x = “”

end if

end forall

end if

end if

if I put a msgbox on the fldcombo line, it displays the content of the arrays (field1 and field2) like this, which shows me that it is getting a handle on the values:

QUE1104|US-1234

QUE1105|US-1235

QUE1104|US-1234

QUE1106|US-1236

QUE1105|US-1235

(the values on the left correspond to field1, and the values on the right are field2)

Every duplicate entry on the left (QUE1104 for example), needs to be matched up with all of its corresponding entries on the right, like this:

QUE1104 = US-1234, US-1234

QUE1105 = US-1235, US-1235

QUE1106 = US-1236

This is where any help would be appreciated. Thanks for your help.

Subject: RE: Help with arrays

The List datatype in LotusScript is an easy way to accumulate such results. In addition, please note the Split and Join functions in LotusScript, and MANY other string and array-based functions, which should save you having to use Evaluate just to manipulate strings and arrays.

Subject: RE: Help with arrays

I appreciate the feedback, and I now see how the split and join functions can replace the @explode and @implode options to make the code cleaner, so to speak, but I’m still not clear about a few things.

The elements corresponding to filed1 are not known in advance, nor are they in any type of sequential order. Even if I used the ArrayUnique function like someone suggested, it would strip the duplicate elements of field1, but it is by the duplicate elements that I know how many elements in field2 are dependent on the dupes in field1. (Hopefully that makes sense.)

Could you further suggest how the List datatype could sort the duplicates in field1 and match them to the elements in field2? That would really, really be helpful. Thanks.

Subject: RE: Help with arrays

Sub Click(Source As Button)’ sample data

Const FLDDAT = {QUE1104|US-1234

QUE1105|US-1235

QUE1104|US-1234

QUE1106|US-1236

QUE1105|US-1235}

’ load the sample data into two arrays

’ NOTE: In a real application you would just read them from their fields and maybe use Split on them if they’re not multivalued already, though why would they not be? There is no need to use Evaluate.

Dim values

values = Split(FLDDAT, {

})

Dim cd1arr, cd2arr

cd1arr = values

cd2arr = values

Dim i%

For i = 0 To Ubound(values)

	cd1arr(i) = Strleft(values(i), "|")

	cd2arr(i) = Strright(values(i), "|")

Next

’ check your inputs.

Msgbox "cd1arr=" & Join(cd1arr, ", ") & {

cd2arr=} & Join(cd2arr, ", ")

’ compile a list where the tag is the value you need to be unique, and the value is a comma-delimited list of the values associated with the values on the left.

Dim results List As String

For i = 0 To Ubound(cd1arr)

	If Iselement(results(cd1arr(i))) Then

		results(cd1arr(i)) = results(cd1arr(i)) & "," & cd2arr(i)

	Else

		results(cd1arr(i)) = cd2arr(i)

	End If	

Next

’ convert the result to a string that can be displayed to the user.

Dim strResult$

Forall thing In results

	strResult = strResult & {

} & Listtag(thing) & " = " & thing

End Forall



Msgbox Mid$(strResult, 2)

End Sub

Subject: RE: Help with arrays

Hi,As Andre suggested there are certian ways to make the arrays unique. you can use dynamic arrays and make the arrays unique.

Example:

ArrayUnique(sourceArray [,compMethod ])

Elements

sourceArray

Array of any type.

compMethod

Optional Integer specifying the type of comparison to use when searching for the delimiter, if the elements are strings.

Number Comparison Mode

0 case sensitive, pitch sensitive

1 case insensitive, pitch sensitive

4 case sensitive, pitch insensitive

5 case insensitive, pitch insensitive

I hope this helps

cheers

RAJ