Skip to main content

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 Tue, 09/11/2018 - 07:51

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… Fri, 11/02/2018 - 10:31

In reply to by Brian Element

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 Sun, 11/04/2018 - 08:38

In reply to by Robert van den…

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… Mon, 11/05/2018 - 11:06

In reply to by Brian Element

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 Mon, 11/05/2018 - 11:39

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

Robert van den… Fri, 11/30/2018 - 10:01

In reply to by Brian Element

I tried to combine two different lists into one. But for some reason the code doens't work and it keeps getting subscript out of range on line 348. In my mind it should work but IDEA thinks diffrently. Where goes my thinking in the wrong direction? What do i wrong?

Brian Element Sun, 12/02/2018 - 14:02

In reply to by Robert van den…

From what I have seen from your script (thanks), the two arrays are coming from differnet sources so they have a potential to be of different sizes.  You only want to do this if the arrays are of the same size or the second one is larger, if the second array is smaller then you will get this error. What you can do is place a msgbox before you try and combine and see the size of each array, if the second one is smaller that explains the problem.

msgbox ubound(listbox3)

msgbox ubound(listbox4)

Robert van den… Fri, 12/07/2018 - 09:44

In reply to by Brian Element

I understand the problem now, in the file on which I'm testing there are more ledger accounts then legder descriptions because some ledger descriptions are generaly used in multiply ledger accounts.
When i create a new variable direct from the dialog droplistbox it countains the row number. But when i want to select and show the content of this row number in a message box it stays blank:
Dim Criteria2 As String
Criteria2 = dlg1.DropListBox5
MsgBox Criteria2 
MsgBox list5$(dlg1.DropListBox5 + 1)
  Is it possible to use the row number to show the content? My idea is to combine two variables and let the user select those. But to use those selected items in IDEA they needed to be seperated again. So I thought i could you the row number of the combined array to select the value from the individual arrays.

The website encountered an unexpected error. Try again later.