Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
the code below will remove duplicate lines and sorting .

i would like to modify the code below to add more function

as

1- if g1,g2,g3,g4 =0 then line will be removed like

DDS,20110523,<big>0,0,0,0</big>,24


2-if g1,g2,g3,g4 =g4 of the line before then line will be removed like

the line as
DDS,20110526,<big>1.1,1.1,1.1,1.1</big>,29
DDS,20110525,2.4,2.7,2.3,<big>1.1</big>,22

the txt file contain more then 3000 lines and different q(clo 0)(name as DDS ,RRB,EES...etc) and date (clo 1)
the code i am using

VB
Public Class AAABB
    Friend Class DataItemList
        Property DataItems As List(Of DataItem)
       

        Friend Class DataItem
            Public q As String
            Public theDate As DateTime
            Public g1 As Double
            Public g2 As Double
            Public g3 As Double
            Public g4 As Double
            Public g5 As Integer

            Overrides Function ToString() As String
                Return String.Format("{0},{1},{2},{3},{4},{5},{6}", q, theDate.ToString("yyyyMMdd"), g1, g2, g3, g4, g5)
            End Function

        End Class

        Sub AddOrUpdate(d As DataItem)
            If DataItems.Exists(Function(x) x.q = d.q AndAlso x.theDate = d.theDate) Then
                Dim idx = DataItems.FindIndex(Function(x) x.q = d.q AndAlso x.theDate = d.theDate)
                DataItems(idx) = d
            Else
                DataItems.Add(d)
            End If

        End Sub


        Sub LoadData(src As String)
            Using tfp = New TextFieldParser(src)
                tfp.TextFieldType = FieldType.Delimited
                tfp.Delimiters = {","}
                tfp.ReadLine() ' skip headers.
                Dim s As String()
                Dim lineNo As Integer = 1 ' we've skipped the first line

                While Not tfp.EndOfData
                    s = tfp.ReadFields
                    If s.Count = 7 Then
                        ' you /should/ parse the data more thoroughly than this, e.g. with TryParse.
                        Me.AddOrUpdate(New DataItemList.DataItem With
                            {.q = s(0),
                             .theDate = DateTime.ParseExact(s(1), "yyyyMMdd", Nothing),
                             .g1 = Double.Parse(s(2)),
                             .g2 = Double.Parse(s(3)),
                             .g3 = Double.Parse(s(4)),
                             .g4 = Double.Parse(s(5)),
                             .g5 = Int32.Parse(s(6))})

                    Else
                        MsgBox(String.Format("Error in file {0} at line {1}.", src, lineNo))
                    End If

                    lineNo += 1

                End While
            End Using
        End Sub

        Sub New()
            DataItems = New List(Of DataItem)
        End Sub

    End Class


    Public Sub TEXTAAA()
        Dim myData = New DataItemList

        myData.LoadData("C:\AAAA.txt")
        myData.LoadData("C:\BBBB.txt")
        Dim sortedData = myData.DataItems.OrderBy(Function(d) d.q).ThenByDescending(Function(d) d.theDate)

        ' show the result
        Dim objWriter As New System.IO.StreamWriter("C:\test.txt")
        objWriter.Write(String.Join(vbCrLf, sortedData))
        objWriter.Close()

        System.Diagnostics.
       Process.Start("notepad", "C:\test.txt")



    End Sub
End Class


thank you for your help
Posted
Updated 25-Apr-13 22:27pm
v4
Comments
OriginalGriff 26-Apr-13 4:24am    
And your problem is?
YOu just say "I want to do this" without saying what is preventing you from doing it!
myproject235 26-Apr-13 4:42am    
i did try but the result wrong

this what i need ?
to add more function

by removing lines ?

as i state above

thank you

YOu just say "I want to do this" without saying what is preventing you from doing it! ?????!!!!!

1 solution

VB
Sub AddOrUpdate(d As DataItem)
    If DataItems.Exists(Function(x) x.q = d.q AndAlso x.theDate = d.theDate) Then
        Dim idx = DataItems.FindIndex(Function(x) x.q = d.q AndAlso x.theDate = d.theDate)
        DataItems(idx) = d
    Else
        If d.g2 = d.g5 And d.g3 = d.g5 And d.g4 = g5 Then
            d.RemoveAt(d.Count - 1) 'Remove the previously added element
        End If
        If Not (d.g2 = 0 And d.g3 = 0 And d.g4 = 0 And d.g5 = 0) Then
            DataItems.Add(d)
        End If
    End If
End Sub


Regards,
— Manfred
 
Share this answer
 
Comments
myproject235 26-Apr-13 7:20am    
thank you for your reply.

If d.g2 = d.g5 And d.g3 = d.g5 And d.g4 = g5 Then

it is

If d.g1 = d.g4 And d.g2 = d.g4 And d.g3 = g4 Then
the curent line like this
DDS,20110526,1.1,1.1,1.1,1.129
g1=1.1 and g2=1.1 and g3=1.1 and g4=1.1

and also g4=1.1 not equal

g4 from the line befor as
DDS,20110525,2.4,2.7,2.3,1.1,22 as you can see g4=1.1

the two line are
DDS,20110526,1.1,1.1,1.1,1.1,29
DDS,20110525,2.4,2.7,2.3,1.1,22

the line(DDS,20110526,1.1,1.1,1.1,1.1,29) should be removed


also this the error message

'g4' is not declared. It may be inaccessible due to its protection level

'RemoveAt' is not a member of 'tasmaq.AAABB.DataItemList.DataItem'.

'Count' is not a member of 'tasmaq.AAABB.DataItemList.DataItem'




Thank you so much for all your efforts

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900