Wrox Programmer Forums
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2003, 12:27 PM
Authorized User
 
Join Date: Jun 2003
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rylemer Send a message via MSN to rylemer Send a message via Yahoo to rylemer
Default 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
 
Old July 22nd, 2003, 12:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How many strings can you have in the file?

Marco
 
Old July 22nd, 2003, 12:47 PM
Authorized User
 
Join Date: Jun 2003
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rylemer Send a message via MSN to rylemer Send a message via Yahoo to rylemer
Default

only 1 string
 
Old July 22nd, 2003, 02:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rylemer
 only 1 string
 
Old July 22nd, 2003, 02:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 22nd, 2003, 03:18 PM
Authorized User
 
Join Date: Jun 2003
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rylemer Send a message via MSN to rylemer Send a message via Yahoo to rylemer
Default

theres a lot of string in the File some are thousands
 
Old July 25th, 2003, 02:49 AM
Authorized User
 
Join Date: Jul 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alex_read
Default

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
 
Old July 25th, 2003, 04:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"


 
Old July 28th, 2003, 04:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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?
 
Old July 29th, 2003, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
JSTL String comparison probem! Plzzz help !! shimmeringtrinkets JSP Basics 0 February 26th, 2008 10:05 AM
String comparison arnabghosh Classic ASP Professional 1 June 29th, 2006 06:01 AM
Fuzzy string comparison (2) kobus XSLT 2 October 11th, 2004 07:28 AM
fuzzy string comparison kobus XSLT 0 September 22nd, 2004 05:47 AM
Please Help - Tricky String Comparison ank2go SQL Server 2000 1 February 6th, 2004 09:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.