rbb Posted December 9, 2004 Posted December 9, 2004 I am trying to get the number of entries per hour in a log file. The data looks like this: 12/5/04 8:15 AM - blah blah blah ... 12/5/04 8:50 AM - blah blah blah ... 12/5/04 10:59 AM - blah blah blah The results should be something like Date - Count 12/5/04 8 AM - 15 12/5/04 9 AM - 21 12/5/04 10 AM - 2 In VB6 I used to create a temporary ADO recordset to summarize data. I would filter by the date, and if a match was found, increment the counter. Otherwise, create a new record and set its counter to one. The question is - what is the proper way to do this same summarizing in .Net? Arraylist, DataSet? And can you please provide an example? Thanks, Rob Quote
mskeel Posted December 9, 2004 Posted December 9, 2004 A hashtable could work well here. You would parse out the date and use that as the key, then increment an intiger you store in the bucket. Something like (this is rough, from the hip, but should give you a general idea.) 'while looping through, reading your log file 'parse out the date and time if hashy.ContainsKey(dateAndTimeYouParsed) then hashy(dateAndTimeYouParsed) += 1 else 'the key is not in, so you must initilize hashy.Add(dateAndTimeYourParsed, 1) end if 'read the next line 'end the loop Quote
neodammer Posted December 9, 2004 Posted December 9, 2004 You could also since that data isnt too congested use a file for datastorage. Quote Enzin Research and Development
rbb Posted December 9, 2004 Author Posted December 9, 2004 You could also since that data isnt too congested use a file for datastorage. I acutally ended up using a dataset. Thank you for the replies though. Rob Dim ds as DataSet ds = New DataSet Dim myColumn As DataColumn Dim d(1) As DataColumn ds.Tables.Add("tmp") myColumn = New DataColumn myColumn.ColumnName = "year-month" myColumn.DataType = GetType(String) myColumn.Unique = True ds.Tables("tmp").Columns.Add(myColumn) d(0) = myColumn myColumn = New DataColumn myColumn.ColumnName = "count" myColumn.DataType = GetType(Integer) myColumn.DefaultValue = 0 ds.Tables("tmp").Columns.Add(myColumn) ds.Tables("tmp").PrimaryKey = d 'And later on... With ds.Tables("tmp") dr = .Rows.Find(my_Date) If dr Is Nothing Then dr = .NewRow dr(0) = my_Date dr(1) = Integer.Parse(my_Counter) .Rows.Add(dr) Else dr(1) += Integer.Parse(Counter) End If End With Quote
mskeel Posted December 9, 2004 Posted December 9, 2004 Looks like that'll work well. It appears to be a lot of overhead, settign up and naming columns, declaring primary keys, etc. A hashtable would take far less code and accomplish the exact same thing...all for free. But, you do know the problem at hand and there may be intricacies I do not understand completely. What does using the dataset buy you? I am not familiar with that data sctructure. Why did you decide to go with it? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.