p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro VB 6 (http://p2p.wrox.com/forumdisplay.php?f=73)
-   -   String Comparison (http://p2p.wrox.com/showthread.php?t=1949)

rylemer July 22nd, 2003 12:27 PM

String Comparison
 
I have to read text file, I use Open as Input
Then When I get the string, I looking for I will place it to another text File, I use Open as Output.
The string that I'll get are:
     A
     A
     B
     B
     C
     C
But only string without Duplicate want to get the such as:
     A
     B
     C

any Idea or any faster functions I'll will use for this because Im reading files for this.

Thanks
Elmer

marcostraf July 22nd, 2003 12:38 PM

How many strings can you have in the file?

Marco

rylemer July 22nd, 2003 12:47 PM

only 1 string

marcostraf July 22nd, 2003 02:54 PM

Quote:

quote:Originally posted by rylemer
 only 1 string

marcostraf July 22nd, 2003 02:57 PM

Sorry for the previous empty post.
Elmer, in your original post there are six strings in the file:

A
A
B
B
C
C


Quote:

quote:Originally posted by rylemer
 only 1 string

rylemer July 22nd, 2003 03:18 PM

theres a lot of string in the File some are thousands

alex_read July 25th, 2003 02:49 AM

I think I've understtod this right & here's my take on the above, let me know if this isn't what you're after:

Code:

Private Const cstrFILETOCHECK As String = "C:\SampleFile.txt"
Private Const cstrFILETOWRITETO As String = "C:\2ndSampleFile.txt"

Private Sub Form_Load()
    Dim straryRetFileLines() As String
    Dim lngElementCount As Long
    Dim blnOkToAddFileLine As Boolean
    Dim strCurrentFileLine As String

    lngElementCount = 0
    blnOkToAddFileLine = False

    Open cstrFILETOCHECK For Input As #1
        Do While Not EOF(1)
            Line Input #1, strCurrentFileLine

            If (lngElementCount = 0) Then
                blnOkToAddFileLine = True
            ElseIf Not (CStr(Trim(strCurrentFileLine)) = straryRetFileLines(lngElementCount - 1)) Then
                blnOkToAddFileLine = True
            Else
                blnOkToAddFileLine = False
            End If

            If (blnOkToAddFileLine = True) Then
                ReDim Preserve straryRetFileLines(lngElementCount)
                 straryRetFileLines(lngElementCount) = CStr(Trim(strCurrentFileLine))

                lngElementCount = lngElementCount + 1
            End If
        Loop
    Close #1

    strCurrentFileLine = ""

    For lngElementCount = 0 To UBound(straryRetFileLines)
        strCurrentFileLine = strCurrentFileLine & straryRetFileLines(lngElementCount) & vbCrLf
    Next lngElementCount

    Open cstrFILETOWRITETO For Output As #2
        Print #2, strCurrentFileLine
    Close #2

    MsgBox "Done!!"
    Unload Me
End Sub


marcostraf July 25th, 2003 04:05 PM

There is just one problem with this code: if the file is huge it is not a very good
idea to allocate all the data in memory. And the Redim Preserve can be very expensive!

Marco

Quote:

quote:Originally posted by alex_read
 I think I've understtod this right & here's my take on the above, let me know if this isn't what you're after:

[code]Private Const cstrFILETOCHECK As String = "C:\SampleFile.txt"
Private Const cstrFILETOWRITETO As String = "C:\2ndSampleFile.txt"




pgtips July 28th, 2003 04:54 AM

I agree with Marco. Something like the Scripting.Dictionary is much better than array for this type of thing.

As an alternative, how about using the Text ODBC driver and running a DISTINCT SQL query against it? Never tried it myself, but its got to be worth a go, hasn't it?

pgtips July 29th, 2003 06:49 AM

Elmer, I don't know if you're interested in going down the text driver route, but it seems to me a simple solution to your problem.

As I said previously, I've never done this myself, but I've now had a chance to try it out and it worked just fine. Here's what I did:
1. created a text file called duplicates.txt in my C:\Temp directory and added duplicate entries for A,B,C as shown in your original post
2. created new VB project and added a reference to ADO
3. added a command button and list box to the form
4. added the following code to the Command1_Click, which uses the Text driver (I actually used the Jet version, but there is also ODBC version) to execute a DISTINCT SQL query against the text file and write the results into the list box.
Code:

Private Sub Command1_Click()

    Dim oConn As ADODB.Connection
    Dim oRs As ADODB.Recordset

    List1.Clear

    Set oConn = New ADODB.Connection

    oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
       "Data Source=c:\Temp\;" & _
       "Extended Properties=""text;HDR=No;FMT=Delimited"""

    Set oRs = New ADODB.Recordset

    oRs.Open "Select DISTINCT * From duplicates.txt", oConn, _
             adOpenForwardOnly, adLockReadOnly, adCmdText

    Do While Not oRs.EOF

        List1.AddItem oRs.Fields(0).Value
        oRs.MoveNext

    Loop

    oRs.Close
    oConn.Close

    Set oRs = Nothing
    Set oConn = Nothing

End Sub

After the code had run, the list box contained only one entry for each of A, B and C.

hth - I certainly learned a new trick today :)
Phil


All times are GMT -4. The time now is 04:51 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.