|
-
Apr 25th, 2001, 03:54 PM
#1
randomizing the lines of a text delimited file
i have a delimited text file with thousands of records
however they are in alpha order,
one line has approx. 8 fields separated by comma's
i want to keep the fields in each line in their order but
mix up the order of the lines themselves....
anyone with an idea?
-
Apr 25th, 2001, 05:50 PM
#2
well first get then into an array.
Code:
Dim tmp As String
Open "C:\file.txt" For Binary As #1
tmp = space(LOF(1))
Get#1,,tmp
Close#1
Dim sLines() As String
sLines = Split(tmp,vbcrlf)
Dim done As String
Open "C:\newfile.txt" For Output As #1
Randomize
Do Until doneit = True
AGAIN:
num = int(Rnd*UBound(slines))
If slines(num) = "" Then GoTo again
Print#1,slines(num)
slines(num) = ""
For x = 0 To UBound(sLines)
If sLines(x) <> "" Then doneit = False:Exit For
doneit = true
Next
Loop
Close#1
ok..that is REALLY off the top of my head...not even tested...
give it a shot and let me know where it blows up! ;)
VBBrowser v2.2.1
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Apr 25th, 2001, 05:52 PM
#3
_______
<?>
Code:
Option Explicit
Dim myString As String
Public Sub MixMeUp()
Dim nTry As Integer
Dim nAddCount As Integer
Dim bFound As Integer
Dim intCre As Integer
Dim nDummy As Integer
Dim MyCollection As New Collection
Dim myArr As Variant
myArr = Split(myString, ",")
nAddCount = UBound(myArr) + 1
Randomize
Do Until intCre = UBound(myArr) + 1
nTry = Int((nAddCount * Rnd)) + 1
On Error Resume Next
nDummy = MyCollection.Item(CStr(nTry))
bFound = (Err = 0)
If bFound Then
Err.Clear
Else
MyCollection.Add nTry, CStr(nTry)
intCre = intCre + 1
End If
Loop
Dim myArray()
Dim i As Integer
ReDim Preserve myArr(1 To nAddCount)
'open your file for output so you can rewrite it
Open "C:\my documents\myfile.txt" For Output As #1
For i = 1 To nAddCount
ReDim Preserve myArray(i)
myArray(i) = MyCollection(i)
Print #1, myArray(i) 'write the new file
Next i
Close #1
End Sub
Private Sub Form_Load()
Dim v As String
Dim myArr()
Dim myFile As String
Dim iNum As Integer
iNum = FreeFile
Dim i As Integer, j As Integer
myFile = "C:\my documents\myfile.txt"
Open myFile For Input As #iNum
'get the line count of your file
Do Until EOF(iNum)
Line Input #iNum, v
i = i + 1
Loop
Close
j = i 'lines in file
'load your file into an array
Open myFile For Input As #iNum
For i = 1 To j
ReDim Preserve myArr(i)
Line Input #iNum, myArr(i)
myString = myString & "," & i
Next i
Close
'load the number of each string into a string seperated by a comma ie..1,2,3,4,etc
'used in collection mix up
myString = Right(myString, Len(myString) - 1)
'change the string order
MixMeUp
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Apr 25th, 2001, 07:51 PM
#4
EEEEEP.
You guys ever read "the Art of Computing"? 3 volumes of algorithms.
90% of the questions about alogrithms in this board are in there - published 1968, by the way.
The one you want for this is called an array:
-- From ALGOL to vb
Sub rand(arr() As String)
Dim limit As Long, x As Long
limit = UBound(arr())
Do While limit > 0 ' assumes option base 1
x = Round(Rnd * limit)
swap arr(x), arr(limit)
limit = limit - 1
Loop
End Sub
Sub swap(i As String, j As String)
Dim tmp$
tmp$ = j
j = i
i = tmp$
End Sub
Maybe reading a really old book would help the "bloatware" thing...
-
Apr 25th, 2001, 07:53 PM
#5
MAybe I should read my posts first....
Algorithm == Randomizing an Array
Duh.
-
Apr 25th, 2001, 09:56 PM
#6
well I feel put down....thanks jim.
just kiddng....
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Apr 26th, 2001, 09:26 AM
#7
thanks everyone for all of your help..
i am ever grateful....
got it to work
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|