-
July 18th, 2007, 08:24 PM
#1
Member
Data Manipulation in VB.NET
I have just started with .NET programming and I had a question as to how I should go about doing this data manipulation.
Its a little hard to explain exactly what I want but bare with me.
What I have are documents that are formatted like so
Code:
Option
$Comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
XPAR 34 55 33
...
Option
$Comment
...
This continues based on simulation that has been run. The data sets all start with Option.
What I want to do is scan through the document line by line (because I'm terrible with regular expressions) and search for "XPAR". If I find "XPAR" I need to delete that entire Option. That is I need to decriment and delete each line back up to Option and then skip foward to the next "Option" and continue to write to a new file.
So the new file will not have the data set "Option" where "XPAR" is found.
My idea was to read line by line into a temporary file until i found Xpar, then search back up and delete lines until i get to Option, skip to the next Option and continue to read line by line. This is the function that I have so far.
Code:
Friend Sub import(ByVal strInputFile As String)
Dim tmpFile As String = Path.GetTempFileName()
Dim fsTemp As New FileStream(tmpFile, FileMode.Create)
Dim sr As StreamReader = New StreamReader(strInputFile)
Dim sw As StreamWriter = New StreamWriter(tmpFile)
While sr.Peek() > -1
sw.WriteLine(sr.ReadLine)
If sr.ReadLine.Contains("CCONE") Then
End If
End While
sr.Close()
sw.Close()
fsTemp.Close()
File.Delete(tmpFile)
End Sub
I don't know a whole lot about FileStreams, StreamReader/Writers so I don't really know how to go about scanning up and deleting while writing to the file.
Any suggestions or tips?
-
July 18th, 2007, 10:13 PM
#2
Hi
If I understand you correctly, each set of "independent" data is flagged
by "Option". If that option is traded at XPAR, you don't want it in the new
file.
So, my suggestion is the following:
1. Read a whole set of lines from the source file "strInputFile" into a string-array or list(of string).
2. Analyse these lines (or do so while reading the lines) whether they contain XPAR
3. If true, clear the list and continue with the next set
4. If false, append the list to the new file, clear the list and continue with the next set.
Simple and no need to delete lines which should not have been written in the first place.
Question: Is there only one trading place per Option-set, or is it possible that there are more?
If so, what do you do if you have a line with XPAR and a line below with XLYO
(I have no idea of your particular data file, so this may be a naive question)?
Cheers.
Last edited by sec_ware; July 18th, 2007 at 10:16 PM.
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
-
July 19th, 2007, 02:28 PM
#3
Member
No matter what else is in that Option-set I do not want it if there is an XPAR So if 1 or more XPAR's are found then DO NOT include that Option-set in the new file.An example of a properly formatted output givin this info would be:
Input:
Code:
Option
$Comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
Grid
Option
$Comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
XPAR 34 55 33
Grid
Option
$comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
Grid
Output:
Code:
Option
$Comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
Grid
Option
$comment
Value 5.5 6.6 77
Value 3.0 .04 .90
DP -2 3 4
Value 02 9 84
Grid
Now keep in mind that the values will change per Option, but I just copied and pasted for sake of time.
But what you have laid out makes more sence than what I was thinking.
If I understand you correctly you are saying to run some sort of loop that takes each option-set and stores them in a string array, then run the test to see if there is an XPAR, and if found do not include it in the final output? If so What would be the syntax that searches for data between "Option" (including the first Option) and the next "Option" > stores that into an array and moves on to the next set of data for storage?
Sorry if this is a little messy, it's a little hard to explain.
-
July 19th, 2007, 03:05 PM
#4
sec_ware has described exactly what I would do.
If the world doesn't stop annoying me I will name my kids ";DROP DATABASE;" and get revenge.
-
July 19th, 2007, 03:19 PM
#5
Member
I am a little confused. Are you saying read the entire document in line by line as one big array of strings? Or read data from "Option" to "Option as 1 string element in a string array? I understand the latter of the two, but I don't know how to store a chunk of lines as 1 element in a string array so that arrStr(0) would equal "Option\n $comment\n Value 5.5 6.6 77\n Value 3.0 .04 .90\n DP -2 3 4\n Grid"
arrStr(1) would then equal the next corresponding Option-set.
-
July 19th, 2007, 03:29 PM
#6
in pseudo code something like this
string variable X
while (not end of file){
read a line
is the line XPAR?
YES: clear X, While(line is not option){read next line}
is the line Option?
YES: add X to output, clear X.
NO: add line to X
}
If the world doesn't stop annoying me I will name my kids ";DROP DATABASE;" and get revenge.
-
July 19th, 2007, 03:35 PM
#7
I hope the document does not delimit by a random amount of spaces... Hopefully those are tabs?
-
July 19th, 2007, 03:41 PM
#8
Hi
Hmm, I am in a good mood, so let me put it together for you:
Code:
Dim strInputFile As String = "c:\input.dat"
Dim strOutputFile As String = "c:\output.dat"
Dim sr As StreamReader = New StreamReader(strInputFile)
Dim sw As StreamWriter = New StreamWriter(strOutputFile)
Dim blnXPAR As Boolean = False
Dim strLine As String = ""
Dim strSetList As List(Of String) = New List(Of String)
'skip entries until the Option-Flag is triggered
While sr.Peek() > -1 And Not strLine.Equals("Option")
strLine = sr.ReadLine()
End While
While sr.Peek() > -1
strSetList.Add(strLine) ' "Option"
strLine = ""
While sr.Peek() > -1
strLine = sr.ReadLine()
If strLine.Equals("Option") Then
Exit While
End If
If Not blnXPAR Then
strSetList.Add(strLine) ' "Option"
End If
If strLine.Contains("XPAR") Then
blnXPAR = True
End If
End While
If Not blnXPAR Then
For Each strLine In strSetList
sw.WriteLine(strLine)
Next
End If
strSetList.Clear()
blnXPAR = False
End While
sr.Close()
sw.Close()
...should do the trick
Remark:
Are you saying read the entire document in line by line as one big array of strings?
Actually, there is a function System.IO.File.ReadAllText, which reads the whole file in a string. Besides a few disadvantages, the main advantage is,
that it is very fast.
Cheers
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
-
July 19th, 2007, 03:58 PM
#9
Member
Thank you I'll try that and see if it gets me through it.
-
July 19th, 2007, 04:39 PM
#10
Member
Sec it worked gloriously. Thank you everyone for the help. I had something simular but for some reason my booleon never got tripped to false and it just kept reading until it hit the end of the document.
Once again thanks so much.
Similar Threads
-
By cheyenne1212 in forum Miscellaneous Security Discussions
Replies: 7
Last Post: February 1st, 2012, 02:51 PM
-
By ThePreacher in forum Miscellaneous Security Discussions
Replies: 17
Last Post: December 14th, 2006, 09:37 PM
-
By -DaRK-RaiDeR- in forum Newbie Security Questions
Replies: 9
Last Post: December 14th, 2002, 08:38 PM
-
By E5C4P3 in forum Miscellaneous Security Discussions
Replies: 5
Last Post: March 7th, 2002, 07:35 AM
-
By antihaxor in forum Non-Security Archives
Replies: 0
Last Post: January 24th, 2002, 05:42 PM
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
|
|