PC Review


Reply
Thread Tools Rate Thread

Calculate Average by Date from Text File

 
 
Alex
Guest
Posts: n/a
 
      22nd Nov 2004
I have a comma separated text file in the form:

11/28/2004 12:00:00, 2.78655354
11/28/2004 12:00:10, 1.9978
11/28/2004 12:00:20, 0.978
11/29/2004 12:00:00, 1.98467
11/29/2004 12:00:10, 3.005

I would like to be able to read the file and then write just the date
and the average value to a to an array and then output it to a text
file. I am not having any problems reading in the file. However, I
can't figure out how to summarize the raw data into an array or
structure or collection. The data should look like this:

11/28/2004 1.9207844
11/29/2004 2.494835

I would imagine it requires a for loop of some sort. I have tried for
a while but with no luck. Hopefully, someone can answer this for me.
Here is what I have so far:

Private Sub btnWriteOutputFile_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnWriteOutputFile.Click
Dim srdCurrent As System.IO.StreamReader
Dim intInputCount, intOutputCount, intTempCount As Integer
Dim strLine As String
Dim strFields() As String
Dim chrDelimiter() As Char = {ToChar(",")}
Dim swriCurrent As System.IO.StreamWriter
ofdInputFile.Title = "What file do you want to summarize"
ofdInputFile.ShowDialog()
srdCurrent = New System.IO.StreamReader(ofdInputFile.FileName)
strLine = srdCurrent.ReadLine

Do Until strLine = Nothing
ReDim Preserve OutputRecords(intInputCount)
strFields = strLine.Split(chrDelimiter)
OutputRecords(intInputCount).RecordDate =
ToDateTime(strFields(0))
OutputRecords(intInputCount).Temperature =
ToSingle(strFields(1))
intInputCount += 1
strLine = srdCurrent.ReadLine
Loop
For intOutputCount = 0 To OutputRecords.GetUpperBound(0)

Summarizing Code Here ?????

Next
sfdOutputFile.Title = "Choose File or Name File to save
summary to"
sfdOutputFile.ShowDialog()
swriCurrent = New
System.IO.StreamWriter(sfdOutputFile.FileName)
For intOutputCount = 0 To OutputRecords.GetUpperBound(0)
swriCurrent.Write(OutputRecord(intCount).RecordDate.ToShortDateString
swriCurrent.Write(",")
swriCurrent.Write(OutputRecord(intCount).Temperature 'This
should be the average
swriCurrent.WriteLine()
Next

srdCurrent.Close()

End Sub
 
Reply With Quote
 
 
 
 
Mark Jones
Guest
Posts: n/a
 
      22nd Nov 2004
look into regex.split


"Alex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a comma separated text file in the form:
>
> 11/28/2004 12:00:00, 2.78655354
> 11/28/2004 12:00:10, 1.9978
> 11/28/2004 12:00:20, 0.978
> 11/29/2004 12:00:00, 1.98467
> 11/29/2004 12:00:10, 3.005
>
> I would like to be able to read the file and then write just the date
> and the average value to a to an array and then output it to a text
> file. I am not having any problems reading in the file. However, I
> can't figure out how to summarize the raw data into an array or
> structure or collection. The data should look like this:
>
> 11/28/2004 1.9207844
> 11/29/2004 2.494835
>
> I would imagine it requires a for loop of some sort. I have tried for
> a while but with no luck. Hopefully, someone can answer this for me.
> Here is what I have so far:
>
> Private Sub btnWriteOutputFile_Click(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles btnWriteOutputFile.Click
> Dim srdCurrent As System.IO.StreamReader
> Dim intInputCount, intOutputCount, intTempCount As Integer
> Dim strLine As String
> Dim strFields() As String
> Dim chrDelimiter() As Char = {ToChar(",")}
> Dim swriCurrent As System.IO.StreamWriter
> ofdInputFile.Title = "What file do you want to summarize"
> ofdInputFile.ShowDialog()
> srdCurrent = New System.IO.StreamReader(ofdInputFile.FileName)
> strLine = srdCurrent.ReadLine
>
> Do Until strLine = Nothing
> ReDim Preserve OutputRecords(intInputCount)
> strFields = strLine.Split(chrDelimiter)
> OutputRecords(intInputCount).RecordDate =
> ToDateTime(strFields(0))
> OutputRecords(intInputCount).Temperature =
> ToSingle(strFields(1))
> intInputCount += 1
> strLine = srdCurrent.ReadLine
> Loop
> For intOutputCount = 0 To OutputRecords.GetUpperBound(0)
>
> Summarizing Code Here ?????
>
> Next
> sfdOutputFile.Title = "Choose File or Name File to save
> summary to"
> sfdOutputFile.ShowDialog()
> swriCurrent = New
> System.IO.StreamWriter(sfdOutputFile.FileName)
> For intOutputCount = 0 To OutputRecords.GetUpperBound(0)
> swriCurrent.Write(OutputRecord(intCount).RecordDate.ToShortDateString
> swriCurrent.Write(",")
> swriCurrent.Write(OutputRecord(intCount).Temperature 'This
> should be the average
> swriCurrent.WriteLine()
> Next
>
> srdCurrent.Close()
>
> End Sub



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate average date tracktraining Microsoft Excel Worksheet Functions 3 10th Sep 2008 07:41 PM
Calculate average based on date and other criteria =?Utf-8?B?S3ljYWp1bg==?= Microsoft Excel Misc 3 14th Jul 2006 10:08 PM
Calculate difference between 2 date and times with average =?Utf-8?B?QWVyeW42MzU=?= Microsoft Excel Misc 1 15th Dec 2005 02:56 AM
Calculate Average Date Steve Microsoft Dot NET 28 3rd Feb 2004 12:45 PM
Calculate Average Date Steve Microsoft VB .NET 2 2nd Feb 2004 03:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:07 PM.