473,785 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String builder (Parsing vertically presented records)

Hi, I just started learning Visual Basic (VB.NET 03) and I need to do
this small program that will read a text file we get from another
company that has survey data, parse it and flatten it out and make
single strings out of the records in it. The major difference between
this space delimited file from the many examples in these groups is
that the data is not presented horizontally, but vertically. You can
see an example below.

What I need from this Visual Basic procedure to do first is to ignore
the first line of the text file (headings). Then, extract the RegID
from the first survey record, get all 25 numbers with their answers
(some of them are blank and some of them are numbers) and convert it
into a single string. After this is done, I need a carriage return so
the next survey record can be flatten out. One thing that might make
this easier is that I do get a line that says "NewRegID" when a new
survey record starts.

I need the final data to look like this:

214555134,1,Y,2 ,N,3,Y,4,1,5,Y, 6,Y,7,Y,8,Y,9,1 ,10,Y,11, ... all the way
to 25,2.
214016421,1,Y,2 ,Y,3,Y, .. and so on.

and it will be saved as a text file.

So far, this is what I have in my command button. This basically opens
the text file with StreamReader, reads it line by line and places it
in a text box called txtStrings. I'm missing the most important part:
the record builder and string creation.
Dim StrFileName As String
strFileName ="C:\Survey01.t xt"
If Not System.IO.File. Exists(StrFileN ame) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.Strea mReader =
System.IO.File. OpenText(StrFil eName)

Dim StrLine As String
StrLine = strRdr.ReadLine ()
Do Until StrLine Is Nothing
txtStrings.Appe ndText(StrLine & vbCrLf)
StrLine = strRdr.ReadLine ()
Loop
strRdr.Close()

Survey1.txt sample:

RegID ItemName Response
214555134 NewRegID
214555134 1 Y
214555134 2 N
214555134 3 Y
214555134 4 1
214555134 5 Y
214555134 6 Y
214555134 7 Y
214555134 8 Y
214555134 9 1
214555134 10 Y
214555134 11 Y
214555134 12 Y
214555134 13 Y
214555134 14 Y
214555134 15 1
214555134 16
214555134 17 Y
214555134 18 Y
214555134 19 Y
214555134 20 Y
214555134 21 1
214555134 22 N
214555134 23 N
214555134 24 1
214555134 25 2
214016421 NewRegID
214016421 1 Y
214016421 2 Y
214016421 3 Y
214016421 4 1
214016421 5 Y
214016421 6
214016421 7 Y
214016421 8 Y
214016421 9 1
214016421 10 Y
214016421 11 N
214016421 12 Y
214016421 13 Y
214016421 14 Y
214016421 15 1
214016421 16 Y
214016421 17
214016421 18 Y
214016421 19 Y
214016421 20 Y
214016421 21 1
214016421 21 1
214016421 22 Y
214016421 23 N
214016421 24 2
214016421 25 3
213565432 1 Y
213565432 2 N
213565432 3 N
...
EOF

Any help would be greatly appreciated it.

Thanks!

Mar 14 '06 #1
18 2165
IL***@NETZERO.N ET wrote:
Hi, I just started learning Visual Basic (VB.NET 03) and I need to do
this small program that will read a text file we get from another
company that has survey data, parse it and flatten it out and make
single strings out of the records in it. The major difference between
this space delimited file from the many examples in these groups is
that the data is not presented horizontally, but vertically. You can
see an example below.

What I need from this Visual Basic procedure to do first is to ignore
the first line of the text file (headings). Then, extract the RegID
from the first survey record, get all 25 numbers with their answers
(some of them are blank and some of them are numbers) and convert it
into a single string. After this is done, I need a carriage return so
the next survey record can be flatten out. One thing that might make
this easier is that I do get a line that says "NewRegID" when a new
survey record starts.

I need the final data to look like this:

214555134,1,Y,2 ,N,3,Y,4,1,5,Y, 6,Y,7,Y,8,Y,9,1 ,10,Y,11, ... all the way
to 25,2.
214016421,1,Y,2 ,Y,3,Y, .. and so on.

and it will be saved as a text file.

So far, this is what I have in my command button. This basically opens
the text file with StreamReader, reads it line by line and places it
in a text box called txtStrings. I'm missing the most important part:
the record builder and string creation.
Dim StrFileName As String
strFileName ="C:\Survey01.t xt"
If Not System.IO.File. Exists(StrFileN ame) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.Strea mReader =
System.IO.File. OpenText(StrFil eName)

Dim StrLine As String
StrLine = strRdr.ReadLine ()
Do Until StrLine Is Nothing
txtStrings.Appe ndText(StrLine & vbCrLf)
StrLine = strRdr.ReadLine ()
Loop
strRdr.Close()

Survey1.txt sample:

RegID ItemName Response
214555134 NewRegID
214555134 1 Y
214555134 2 N
214555134 3 Y
214555134 4 1
214555134 5 Y
214555134 6 Y
214555134 7 Y
214555134 8 Y
214555134 9 1
214555134 10 Y
214555134 11 Y
214555134 12 Y
214555134 13 Y
214555134 14 Y
214555134 15 1
214555134 16
214555134 17 Y
214555134 18 Y
214555134 19 Y
214555134 20 Y
214555134 21 1
214555134 22 N
214555134 23 N
214555134 24 1
214555134 25 2
214016421 NewRegID
214016421 1 Y
214016421 2 Y
214016421 3 Y
214016421 4 1
214016421 5 Y
214016421 6
214016421 7 Y
214016421 8 Y
214016421 9 1
214016421 10 Y
214016421 11 N
214016421 12 Y
214016421 13 Y
214016421 14 Y
214016421 15 1
214016421 16 Y
214016421 17
214016421 18 Y
214016421 19 Y
214016421 20 Y
214016421 21 1
214016421 21 1
214016421 22 Y
214016421 23 N
214016421 24 2
214016421 25 3
213565432 1 Y
213565432 2 N
213565432 3 N
..
EOF

Any help would be greatly appreciated it.

Thanks!


Maybe this will help you. You probably want to use the String.Split
function to get the indvidual items.

Dim StrFileName As String
strFileName ="C:\Survey01.t xt"
If Not System.IO.File. Exists(StrFileN ame) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.Strea mReader =
System.IO.File. OpenText(StrFil eName)
Dim StrLine As String
Dim StringArray() as String
Do strRdr.Peek <> -1
StrLine = strRdr.ReadLine ()
if StrLine.EndsWit h(NewRegID) then
'This is a new Record
StringArray = StrLine.Split(" ")
'StringArray(0) is your new ID
else
'This continuing record
StringArray = StrLine.Split(" ")
'String(0) has Column1
'String(1) has Column2
'String(2) has Column3 (if there is one)
for ii as integer = 1 to String.Getupper bound(0)
'Append your string here
next
end if
Loop
strRdr.Close()
Mar 14 '06 #2
Hi EOF

Do you mean something as (typed here not checked so watch typos or whatever)

Dim StrLine As String
StrLine = strRdr.ReadLine () dim sb as new text.stringbuil der(StrLine.Sub string(0,9))Do Until StrLine Is Nothing dim fields() = StrLine.Split()
sb.append(field s(1) & fields(2)) StrLine = strRdr.ReadLine () if Is StrLine Not Nothing AndAlso
strline.indexof ("NewRegID") > -1 then
strWrt.WriteLin e(sb.ToString)
sb = new text.Stringbuil der(StrLine.Sub string(0,9))
end if Loop
strRdr.Close()


I hope this helps,

Cor
Mar 14 '06 #3
Hi,

Another implementation :

=============== ====
Private Sub ReadText()
Dim fsr As New FileStream("Ver tFile.txt", FileMode.Open,
FileAccess.Read )
Dim fsw As New FileStream("Res ultFile.txt", FileMode.Create ,
FileAccess.Writ e)
Dim sr As New StreamReader(fs r)
Dim sw As New StreamWriter(fs w)

Dim thisLine As String
Dim lineContents() As String

'Ignore the first line of the text file
sr.ReadLine()
While sr.Peek > -1
thisLine = sr.ReadLine()
lineContents = thisLine.Split( New Char() {" "c})
If lineContents(1) .Trim = "NewRegID" Then
'We have a new record. Write current value of sb and Insert new
line.
If sb.Length > 0 Then
'Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb .ToString())
End If
sb = New StringBuilder()
sb.Append(lineC ontents(0))
sb.Append(",")
Else
'This is the same record. Keep adding text
sb.Append(lineC ontents(1))
sb.Append(",")
If sb.Length > 2 Then
sb.Append(lineC ontents(2))
sb.Append(",")
End If
End If
End While
'Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb .ToString())
sw.Flush()
'Clean up
sw.Close()
sr.Close()
fsr.Close()
fsw.Close()
End Sub
=============== ====

Note that your third set of records (213565432) does not have the line
"NewRegID".
When that line is inserted in the sample text, then it works.

Regards,

Cerebrus.

Mar 14 '06 #4
Hi again, thanks for replying.

I'm trying Cerebrus code right now, but when I debug I get these
errors:

Name 'sb' is not declared
Type 'FileStream' is not defined
Type 'StreamReader' is not defined

Am I missing something since these FileStream and StreamReader names
don't seem to be recognized by VB? Does the variable "sb" actually is
meant to be "sr" or is it for the text box?

I'm running VB.net version 2003.

The third and all subsequent records do have the "NewRegID" so that's
not a problem.

Thanks.

Mar 14 '06 #5
You needs some imports in the top of your program

imports system.text
imports system.io

Or put that text and io before where that streamreader and stringbuilder are
used.
text.stringbuil der

Cor

<IL***@NETZERO. NET> schreef in bericht
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
Hi again, thanks for replying.

I'm trying Cerebrus code right now, but when I debug I get these
errors:

Name 'sb' is not declared
Type 'FileStream' is not defined
Type 'StreamReader' is not defined

Am I missing something since these FileStream and StreamReader names
don't seem to be recognized by VB? Does the variable "sb" actually is
meant to be "sr" or is it for the text box?

I'm running VB.net version 2003.

The third and all subsequent records do have the "NewRegID" so that's
not a problem.

Thanks.

Mar 14 '06 #6
Hi, when I added the lines to the top of my procedures:

imports system.text
imports system.io

I got a Syntax error for the 2 of them.

Therefore, I had to change the first line to this:
Dim fsr As New System.io.FileS tream("c:\surv0 60313a.txt",
IO.FileMode.Ope n)

I still get a "name 'sb' is not declared" error for every time the line
shows in the code.

Mar 14 '06 #7
Yes

It is missing, if you look in my sample I have showed you than you see.

dim sb as new text.stringbuil der(StrLine.Sub string(0,9))

in this case you can set it with the declarations in the style Cerebrus
does.

dim sb as new text.stringbuil der()

Cor

<IL***@NETZERO. NET> schreef in bericht
news:11******** **************@ j52g2000cwj.goo glegroups.com.. .
Hi, when I added the lines to the top of my procedures:

imports system.text
imports system.io

I got a Syntax error for the 2 of them.

Therefore, I had to change the first line to this:
Dim fsr As New System.io.FileS tream("c:\surv0 60313a.txt",
IO.FileMode.Ope n)

I still get a "name 'sb' is not declared" error for every time the line
shows in the code.

Mar 14 '06 #8
Hello Cor, now I am trying your code. I'm sorry if I sound way too
frustrated, but I've been trying to do this for days now.

This is what I have in my command button:

=============== ===========

Dim StrFileName As String
StrFileName = "C:\Survey01.tx t"
If Not System.IO.File. Exists(StrFileN ame) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim StrLine As String
Dim strRdr As System.IO.Strea mReader =
System.IO.File. OpenText(StrFil eName)
Dim sb As New System.text.Str ingBuilder(StrL ine.Substring(0 , 9))

StrLine = strRdr.ReadLine ()
Do Until StrLine Is Nothing
Dim fields() = StrLine.Split()
sb.append(field s(1) & fields(2))

if Is StrLine Not Nothing AndAlso strline.indexof ("NewRegID") > -1
then
strWrt.WriteLin e(sb.ToString)
sb = New System.text.Str ingBuilder(StrL ine.Substring(0 , 9))
End If

Loop
strRdr.Close()

=============== ===========
I am getting an "expression expected" error in the last IF Statement:
if Is StrLine Not Nothing AndAlso strline.indexof ("NewRegID") > -1 then

strWrt.WriteLin e(sb.ToString)
sb = New System.text.Str ingBuilder(StrL ine.Substring(0 , 9))
End if
Also, I am getting a "Name strWrt is not declared" error because the
strWrt was not declared.
I tried to declared it as a string, but I got a 'writeline' is not a
member of sTring.

Finally, when I am supposed to append the outcome to my text box (
txtStrings) so I can see it?
Please advise me what to do next.

Thanks.

Mar 14 '06 #9
It's me again, I took another look at those survey text files and I
realized that what separates the columns is not a space, but a (^T)
character. Consequently, this is what one line is made of
214555134^T1^T Y^P

Where ^T is the "space" between the first, second and third columns.
The last character for each line is a paragraph break.

Hope this helps.

Mar 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3026
by: BStorm | last post by:
I have a transaction log file where the DataSet table's Description column is actually delimited into "subcolumns" based upon the transaction id. I would like to parse these into separate fields for reporting purposes and am wondering if anyone knows if this is easily accomplished using the .NET version of Crystal Reports? For example, the description column may be reporting on a dataentry error as follows: TXNCODE: 1010001
11
3920
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
5
5044
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page that calls the web service to render a vertical strip of images. After doing some research I am unable to find some vb.net code that can assist in what I want to achieve. The closest thing I found was
2
9172
by: ILCSP | last post by:
Hi, I have a sql table containing the answers for some tests. The information in this table is presented vertically and I need to create strings with them. I know how to read the data in VB.Net and use a StreamWriter to build the strings. However, the problem lies with the reading of each row. Most of the test takers don't give answers to ALL of the items in a test, but those unanswered items need to be accounted for by using a comma...
12
1764
by: Lennart Anderson | last post by:
I'm having a MySQl table wih a lot of information. I want to present some main fields in a table on one page. Each record do, of course, have a unique ID. The presnted table will have one field as a linked field. I want to be able to click this link, retreive the ID information for that record and then present detailed data for that record on the next page. How do I retreive the ID? Any hints are very much appreciated. Thanks
3
1482
by: Looch | last post by:
Hi All, Not sure if parsing is the correct word but here is what I'm trying to do: I have a method that returns a dataset that uses a select statement that comprises three union all'd select statements (i.e. "select author from table1 union all select author from table 2..."). The end result is a small dataset with one column and four rows
10
18638
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
11
303
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.