Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 337

# Using arrays to manage and sort data

Hello,

I am writing a program that will take a series of data inputs from a .txt file, put the data into an array (or arrays), and then be able to sort it.

The info in the text file looks like this:

0022, "D", 000155.04, "23SEP95"
0001, "D", 000176.97, "16JAN02"
0002, "C", 000023.96, "24JAN99

the first column of numbers are "customer numbers" - they are what needs to be sorted into numerical order.

What I can't figure out, is if I can put the data into one array, or put all the customer numbers into an array, and then put the second column of data into a second array, and so on.

...Or do I make an array for the first line of data, and a second array for the next line, and so on?

A portion of the code that I have so far:

Open App.Path & "\cs261as04sp03T" For Input As #1

'determine amt of data
numcustomers = 0
Do While Not EOF(1)
Input #1, custnumber
numcustomers = numcustomers + 1
Loop
Close #1

'create arrays for the data
Dim custnumber(1 To numcustomers) As String
Dim CorD(1 To numcustomers) As String
Dim custbalance(1 To numcustomers) As curency
Dim chardate(1 To numcustomers) As String

Open App.Path & "\cs261as04sp03T" For Input As #1

For k = 1 To numcustomers
Input #1, custnumber(k), CorD(k), custbalance(k), chardate(k)
Next k
Close #1

However, I get a compiler error "Constant expression required."

Any help is much appreciated,
Justin
0
JustinBGSU
2 Solutions

Senior DBACommented:
There are many options here, but what I would do is to make an array of User Defined Types:

Type CustRec
CustNumber As String * 4
CorD as String * 1
CustBalance As Currency
CharDate As String * 7
End Type

Private Records() As CustRec

Private sRec As String
Private sWork() As Strinig

Then, using your code as a base, change to use Line Input, and ReDim dynamically:

Redim Records(0)
Open App.Path & "\cs261as04sp03T" For Input As #1

Line Input #1, sRec
Do While Not EOF(1)
NumCustomers = NumCustomers + 1
sWork = Split(sRec,",")
Redim Preserve Records(0 to NumCustomers)
With Records(NumCustomers)
.CustNumber = sWork(0)
.CorD = sWork(1)
.CustBalance = CCur(sWork(2))
.CharDate = sWork(3)
End With
Line Input #1, sRec
Loop
Close #1

Then, to sort, use the LSet command:
Dim I as Integer, J as Integer, Ptr as integer

For I = 1 to UBound(Records) - 1
Ptr = I
For J = I + 1 To UBound(Records)
If Records(J).CustNumber < Record(Ptr).CustNumber Then Ptr = J
Next J
If I <> Ptr Then
LSet Records(0) = Records(I)
LSet Records(I) = Records(Ptr)
LSet Records(Ptr) = Records(0)
End If
Next I

Now, the array is sorted from smallest to greatest.
0

Commented:
ok....use a 2 D array.....since there are only 4 things, this should be easy to do

dim array1() as String

Do while not EOF(1)
inI = inI + 1
Redim Preserve array1(1 to 4, 1 to inI) as string
Input #1, array1(1, inI)
Input #1, array1(2, inI)
Input #1, array1(3, inI)
Input #1, array1(4, inI)
Loop

Now, the data is in.

Next conver the first number to a value.

For i = 1 to inI
array1(1, i) = cint(array1(1,i))
Next i

Now sort.  My next post will have the sort in it.
0

Commented:
This is an extrememly fast sort.  Implement as you wish.

Private Sub cmdSort_Click()
Dim i As Integer
Dim BoolSwap As Boolean
Dim inNumVals As Integer
Dim inI() As Integer
inNumVals = 10
ReDim inI(1 To inNumVals) As Integer
For ii = 1 To inNumVals - 1
BoolSwap = False
For i = 1 To inNumVals - ii
If array1(i) > array1(i + 1) Then
Call Swap(inI(), i)
BoolSwap = True
End If
Next i
If BoolSwap = False Then ii = inNumVals - 1
Next ii
For i = 1 To inNumVals
Picture1.Print inI(i)
Next i
End Sub

Private Sub Swap(inI() As Integer, i As Integer)
Dim inTemp As Integer
inTemp = inI(i)
inI(i) = inI(i + 1)
inI(i + 1) = inTemp
End Sub
0

Commented:
Hi JustinBGSU,
This old question (QID 20565667) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Split between bhess1 and Jacamar