By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,268 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,268 IT Pros & Developers. It's quick & easy.

Parsing Mixed length file lines

P: n/a
I am building a parser to parse out segments, then fields, from atext file. It is possible that not all of the segments/fieldswill be present in each line. I have successfully parsed thefile, but now find that if a line contains "extra" segments thatare not in the previous line they do not parse OR if the segmentappears in the original (1st line) then the same data isrepeated in the next (or all) subsequent lines that do not havethe segment present.

My process for this is
1. To utlize the REGEX Object and parse the segements into an1d Array then take that data and place it into a 2d array (oneindex per row in the file).
2. Put the items from the 2d Array into a dataset.datatable
3. Take the elements form the dataset.datatable and place theminto tables on my SQL Server.

I have tried clearing the values from the 1dArray after postingthem to the 2d Array, but this does not work.

The code segment in question, I believe, follows:

'Step 1: Open the file passed in and Parse the firstline
oReaderSegment = oFileSegment.OpenText(sFileName)
sContent = oReaderSegment.ReadLine()

While sContent <> ""
'Step 2: Identify the pull the segments and loadinto 2d-Array
For Each oMatchSegment InoRegexSegment.Matches(sContent)

'load the segment into the temp stringvariable
sTemp = sContent.Substring(iStart,oMatchSegment.Index - iStart)

'identify the segment being processed
sSegmentID = sTemp.Substring(0, 3)

'get the segment location, 0-9 based on acomplete, all segments being present
'as listed in the Type 17 file specification
iSegmentNumber =SetSegmentLocation(sSegmentID)

'load the segment into the 1d-Array
TempSegmentArray(iSegmentNumber) = sTemp

'increment the index to pull the nextsegment
iStart = oMatchSegment.Index + 1

'Step 3: Copy each line segment into thecorresponding element and line
' in a 2D-Array for further processing
For j = 0 To (TempSegmentArray.Length - 1)
sTemp2 = TempSegmentArray(j)
FinalSegmentArray(i, j) = sTemp2
Next j

'read the next line of the file
sContent = oReaderSegment.ReadLine

'increment the counting variable
i += 1

'reset the the element position variable
iStart = 0
'increment the action control variable

End While

Catch ex As Exception

iError = MsgBox(Err.Number & vbCrLf & Err.Description& vbCrLf & _
"This error occured in the SegmentParser Portion ofthe code", MsgBoxStyle.Critical, "There's Been a Segment ParsingError")

'Do housekeeping by clearning all objects
oRegexSegment = Nothing
oMatchSegment = Nothing
oMatchesSegment = Nothing
oFileSegment = Nothing
oReaderSegment = Nothing
TempSegmentArray = Nothing

End Try

Thanks for any suggestions.
From: Michael Tognetti

Posted by a user from .NET 247 (

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.