Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a file.

Status
Not open for further replies.

Denk

Programmer
Jan 4, 2000
22
0
0
GB
I am attempting to sort a file which consists of a list of strings which contain two numerical variables stored per line of text. The variables are stored in the following format "0000" and they take the form of a basic x,y coordinate in a 32*40 grid.

The two numeric variables can range in value from 101 to 3240. I came up with the following routine, but it is very inefficient:-

For f = 1 To 32
For g = 1 To 40
For h = 1 To 32
For i = 1 To 40
Open currentdirectory$ & "\TradeRoutes2.TMP" For Input As #1
Do Until EOF(1)
Line Input #1, lineoftext$
If Format(f, "00") & Format(g, "00") = Mid$(lineoftext$, 1, 4) And Format(h, "00") & Format(i, "00") = Mid$(lineoftext$, 8, 4) Then
traderoutelist$ = traderoutelist$ & lineoftext$ & NL
Exit Do
End If
Loop
DoEvents
Label2.Caption = f & " " & g & " " & h & " " & i
Close #1
Next i
Next h
Next g
Next f

Can anyone come up with a suggestion for a better and more efficient routine? [sig][/sig]
 
First, [red]DO NOT[/red] open/read/close a file in a loop.

Open the file.
Read all of it (into something internal - probably an array)
Sort the internal thinggy

No matter how inefficient the sort (at nleast as long as it does sort), it will not take very long, so efficiency - without the file I/O will be O.K.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
1.6 million open file commands!!!! - As MichaelRed says, open the file once, read into an array, process the array and then write out to file - 2 open file commands (or 1 if you don't close it the first time, but probably better to open twice in different modes), a lot more efficient.

Simon [sig][/sig]
 
DENEK,

I have cobbled together a variation on your function:


Public Function basReadSort(MyDir As String)

Dim MyArray(1280) As String * 12
Dim TradeRouteList As String
Dim SortFlag As Boolean
Dim MyLine As String
Dim MyFil As Integer
Dim Idx As Integer
Dim Jdx As Integer
Dim Kdx As Integer
Dim Ldx As Integer

'Sample input must be &quot;XXXX YYYY&quot;,
'Where XXXX is the first string and YYYY is the second string
'with four spaces between
Open MyDir & &quot;\TradeRoutes2.TMP&quot; For Input As #MyFil

'Read the input File & Get the info into the Array
Do Until EOF(MyFil)

Line Input #MyFil, MyLine
'Read Values into Array Here
MyArray(Idx) = MyLine

Idx = Idx + 1 'Increment the Array Index

Loop

Close #1

ReDim Preserve MyArray(Idx - 1) 'Reset array size to only active elements

'Sort the Array an post the results in the TradeRoute
While (Not SortFlag)
For Jdx = 0 To UBound(MyArray) - 1
For Kdx = Jdx + 1 To UBound(MyArray)
SortFlag = True 'Assume the array is sorted

If (MyArray(Jdx) > MyArray(Kdx)) Then 'It is not Sorted; Swap J & K

TradeRouteList = MyArray(Jdx) 'J to Temp
MyArray(Jdx) = MyArray(Kdx) 'K to J
MyArray(Kdx) = TradeRouteList 'Temp to K
SortFlag = False 'Set Flag to force another round

End If

'Show the User where we are in the process
' Label2.Caption = Jdx & &quot; &quot; & Kdx

DoEvents

Next Ldx
Next Kdx
Wend

MyFil = FreeFile
Open &quot;TradeRoute.Txt&quot; For Output As #MyFil
'Need to output the list here
For Idx = 0 To UBound(MyArray)
Print #MyFil, MyArray(Idx)
Next Idx
Close #MyFil

End Function


While it looks very different, I beleive it will more-or-less do what you intended. One significant variation is that your function did not sort the input - but simply listed the matches in the indicies in order of the indicies, where the offered routine should list all of the entries in the input in AN order. This will include any typo errors in the input, so you might want to 'look' at the output list to note any improperly formatted entries.

I did not have any specific data to use as a test, so this is just a first cut at it. Also note that this &quot;sort&quot; routine is not very efrficient, however - as proviously stated, the sort process for a small number of entries is not very important - as long as it works.
[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Thanks to everyone who gave advice. I have managed to create a much more efficient routine. [sig][/sig]
 
cant you read it into a listbox with the sorted property set to true then write the listbox to file?

or some variation splicing the input lines to 2 listboxes?? [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top