|
-
Apr 25th, 2013, 07:11 PM
#1
Thread Starter
New Member
modfiy code
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,0,0,0,0,24
2-if g1,g2,g3,g4 =g4 of the line before then line will be removed like
the line as
DDS,20110526,1.1,1.1,1.1,1.1,29
DDS,20110525,2.4,2.7,2.3,1.1,22
the txt file contain more then 3000 lines and different q(clo 0)(name as DDS ,RRB,EES...etc) and date (clo 1)
thanks
Last edited by awadh; Apr 26th, 2013 at 06:55 AM.
-
Apr 26th, 2013, 03:25 AM
#2
Thread Starter
New Member
Re: modfiy code
is there any help
thank you
-
Apr 26th, 2013, 08:50 AM
#3
Re: modfiy code
Hi,
You will notice that you have not had many responses as yet and I would suggest that this is because you are not making a lot of sense with your question which can be interpreted in a number of ways. So to get the ball rolling, here is some example code based on the following interpretation of your post:-
You need to loop through a file and remove the records where the values in columns 3, 4, 5 and 6 are all the same. That being either 0 or any other value.
If that is the case, then this example code reads your file line by line into an array, iterates it contents and checks for duplicate values in columns 3, 4, 5 and 6. If the values are NOT duplicated then the line that has been read from your file is written to a new file otherwise it is discarded as a duplicate?
Code:
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Read each line of your file into an Array
Dim AllLinesInYourFile() As String = File.ReadAllLines(Application.StartupPath & "\ExampleData.txt")
'Declare a file to write the results to
Using myWriter As New StreamWriter(Application.StartupPath & "\ResultingData.txt")
'Iterate each line that was read from your file and test the conditions that you are looking for
For Each strLineInFile As String In AllLinesInYourFile
Dim RecordFields() As String = strLineInFile.Split(","c)
'If the condition does NOT meet the information that you want to ignore then write the VALID line to a new file
If Not (RecordFields(2) = RecordFields(5) AndAlso RecordFields(3) = RecordFields(5) AndAlso RecordFields(4) = RecordFields(5)) Then
myWriter.WriteLine(strLineInFile)
End If
Next
End Using
End Sub
Hope that helps.
Cheers,
Ian
Last edited by IanRyder; Apr 26th, 2013 at 08:55 AM.
-
Apr 26th, 2013, 08:53 AM
#4
Re: modfiy code
Well first, let's add the conditional logic.
For the = 0:
Code:
If g1 = 0 AndAlso g2 = 0 AndAlso g3 = 0 AndAlso g4 = 0 Then
For the = g4. Notice I leave out g4 = g4, because it always will unless the original g4 changes.
Code:
If g1 = g4 AndAlso g2 = g4 AndAlso g3 = g4 Then
Now lets work out the string.replace:
For the If = 0:
Code:
Dim str As String = "DDS,20110523,0,0,0,0,24"
str = str.Replace(",0", "")
MessageBox.Show(str)
For the If = g4:
Code:
Dim str As String = "DDS,20110526,1.1,1.1,1.1,1.1,29" & Environment.NewLine & "DDS,20110525,2.4,2.7,2.3,1.1,22"
str = str.Replace(",1.1", "")
MessageBox.Show(str)
-
Apr 26th, 2013, 09:02 AM
#5
Re: modfiy code
When looking at multiple variables being equal to 'some value' (0 in this example) I like to use this form
Code:
If g1 = 0 AndAlso g2 = g1 AndAlso g3 = g1 AndAlso g4 = g1 Then
End If
That way if 'some value' changes I only have to change a little bit of code. The other option is to use another variable or constant for 'some value'.
-
Apr 26th, 2013, 05:25 PM
#6
Thread Starter
New Member
Re: modfiy code
 Originally Posted by dbasnett
When looking at multiple variables being equal to 'some value' (0 in this example) I like to use this form
Code:
If g1 = 0 AndAlso g2 = g1 AndAlso g3 = g1 AndAlso g4 = g1 Then
End If
That way if 'some value' changes I only have to change a little bit of code. The other option is to use another variable or constant for 'some value'.
thank you for your reply
and this the code i use
Code:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Button
Imports Microsoft.VisualBasic.FileIO
Imports System.IO
Public Class AAABB
Friend Class DataItemList
Property DataItems As List(Of DataItem)
Friend Class DataItem
Public ti As String
Public theDate As DateTime
Public o As Double
Public h As Double
Public l As Double
Public cl As Double
Public vo As Integer
Overrides Function ToString() As String
Return String.Format("{0},{1},{2},{3},{4},{5},{6}", ti, theDate.ToString("yyyyMMdd"), o, h, l, cl, vo)
End Function
End Class
Sub AddOrUpdate(d As DataItem)
If DataItems.Exists(Function(x) x.ti = d.ti AndAlso x.theDate = d.theDate) Then
Dim idx = DataItems.FindIndex(Function(x) x.ti = d.ti 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
{.ti = s(0),
.theDate = DateTime.ParseExact(s(1), "yyyyMMdd", Nothing),
.o = Double.Parse(s(2)),
.h = Double.Parse(s(3)),
.l = Double.Parse(s(4)),
.cl = Double.Parse(s(5)),
.vo = 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.ti).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")
' unselect the text for readability
'TextBox1.SelectionLength = 0
'TextBox1.SelectionStart = 0
End Sub
End Class
this code will remove duplicate line and will sort them
what you provied is good but what i need to get to is
1- if g1,g2,g3,g4 =0 then line will be removed like
DDS,20110523,0,0,0,0,24
2-if g1,g2,g3,g4 =g4 of the line before then line will be removed like
the line as
DDS,20110526,1.1,1.1,1.1,1.1,29 in this line you will see 1.1 in array 2 and also in array 3,array 4,array 5 (array 1=g1,array 2=g2 ........)
but in the line below that will be after the line above
DDS,20110525,2.4,2.7,2.3,1.1,22 in this line 1.1 is in (array 5 )
if any line that array 2 =array 3= array 4 = array 5 and the line after contain the same value in array 5 the line will be removed
but if any line that array 2 =array 3= array 4 = array 5 and the after contain different value in array 5 the line will not be removed like
DDS,20110526,33,33,33,33,29
DDS,20110525,2.4,2.7,2.3,40,22
in the frist line array 2 to array 5 is the same value
but array 5 in the second line is different
i hope my explanation is clear
and thank you again for your time and your understanding
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
|