Array data

6 posts / 0 new
Last post
Robert van den ...
Offline
Joined: 08/08/2018 - 07:37
Array data

I created some code to import a txt file and remove the " symbol from the array:
 
 Function ImportText1
Const fsoForReading = 1
Dim objFSO
Dim objTextStream
Dim FirstTime As Boolean
Dim FileName As String
Dim i As Integer
 
FileName = WorkingDirectory & "\Exports.ILB" & "\Temp1.DEL"
FirstTime = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(FileName, fsoForReading)
Do Until objTextStream.AtEndOfStream 
If FirstTime Then
FirstTime = False
ReDim DataList1$(0)
Dim TempText (0) As String
DataList1$(0) = objTextStream.ReadLine 
Else
ReDim preserve DataList1$(UBound(DataList1$) + 1) 
DataList1$(UBound(DataList1$)) = objTextStream.ReadLine 
End If
Loop
 
Set objTextStream = Nothing
Set objFSO = Nothing
 
DlgListBoxArray "ShowSampleRecord", DataList1$()
Kill WorkingDirectory & "\Exports.ILB" & "\Temp1.DEL"
 
Dim TempArray1$()
ReDim TempArray1$(UBound(DataList1$))
For i = 0 To UBound(DataList1$)
TempArray1$(i) = Left(DataList1$(i), Len(DataList1$(i)) - 1)
Next i
 
Dim TempArray2$()
ReDim TempArray2$(UBound(TempArray1$))
For i = 0 To UBound(TempArray1$)
TempArray2$(i) = Right(TempArray1$(i), Len(TempArray1$(i)) - 1)
Next i
 
ReDim DataList2$(UBound(TempArray2$))
For i = 0 To UBound(TempArray2$)
DataList2$(i) = TempArray2$(i)
Next i
DlgListBoxArray "Show2", DataList2$()
End Function
 
This is probably not the most efficient way, so my question is how can i make the code more efficient? 
 

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Robert, 

You can make the code more efficient by changing all the code after the Kill command to:

Dim DataList2$(UBound(DataList1$))

For i = 0 To UBound(DataList1$)
           DataList2$(i) = Mid(DataList1$(i), 2, Len(DataList1$(i)) - 2)
Next i

Using the mid you can do the right and left at the same time.  I also got rid of the redim since you know how many elements will be in your array you can define the array in one line.  You only need to use the redim when you don't know the exact number of elements but want to set-up a holding array variable.

Brian

Robert van den ...
Offline
Joined: 08/08/2018 - 07:37

Hi, i got the part of deleting the " text working. But is there a way to use the For i - Next i to combine two different arrays to a new array?

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Robert,

There are no easy ways to combine an array in IDEAScript like there are in other languages.  What you need to do is create the new array and then using a loop add the items from the two arrays to the new array.  Here is an example for you.


Sub Main
	Dim Array1(5) As Integer
	Dim Array2(5) As Integer
	Dim CombinedArray() As Integer
	Dim i As Integer
	Dim j As Integer
	Dim iCombinedArrayBounds As Integer
	
	'populate the two intergers
	For i = 0 To 5
		Array1(i) = i
		Array2(i) = i + 6
	Next i
	'Array 1 now holds 0 to 5 and array 2 holds 6 to 11 for 12 items
	'redifine the combined array with 12 items (6 from array 1 and 6 from array 2)
	'calculate the upper bound of the new combined array
	iCombinedArrayBounds = UBound(array1) + UBound(array2) + 1

	ReDim CombinedArray(iCombinedArrayBounds)
	j = 0
	'add items from array 1 to combined array
	For i = 0 To UBound(Array1)
		CombinedArray(j) = Array1(i)
		j = j + 1
	Next i
	'add items from array 1 to combined array
	For i = 0 To UBound(Array2)
		CombinedArray(j) = Array2(i)
		j = j + 1
	Next i
	'display results of new combined array
	For i = 0 To UBound(CombinedArray)
		MsgBox CombinedArray(i)
	Next i

End Sub
Robert van den ...
Offline
Joined: 08/08/2018 - 07:37

Thanks for the info, with the following code i solved my first problem of presenting combined field data to the user.
 
Sub Main
Dim Array1(0) As String
Dim Array2(0) As String
Dim CombinedArray() As String
Dim i As Integer
Dim j As Integer
Dim iCombinedArrayBounds As Integer
 
'populate the two intergers
For i = 0 To 0
Array1(i) = "Robert"
Next i
 
For i = 0 To 0
Array2(i) = "Bogaard"
Next i
 
ReDim CombinedArray(iCombinedArrayBounds)
 
'add items from array 1 and array 2 to the combined array
For i = 0 To UBound(Array1)
CombinedArray(i) = Array1(i) & " - " & Array2(i)
Next i
 
'display results of new combined array
For i = 0 To UBound(CombinedArray)
MsgBox CombinedArray(i)
Next i
 
End Sub

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Robert, sorry, I misunderstood your problem but I am glad you got it to work.  So what was your second problem?