Solved

Parse and save

Posted on 2011-09-17
13
269 Views
Last Modified: 2012-05-12
Hi, Experts -
I need help with handling a (potentially) long character string that represents all of the results submitted by a survey taker.
Below is a sample of such a string.
In this particular survey, there are seven questions delimited by three (3) pound-signs (###).
The values passed for each question are separated by commas, and include:
1. Survey title,
2. Survey date,
3. Survey response time (unique for each question),
4. Survey Item,
5. Survey Item Type,
6. Survey Item Response(s) - multiple responses separated by |#|
Again - This is all passed to me as one single string of characters.
QUESTION:
I need to save these elements into a table that has columns defined with the same 6 elements as the values listed above.  Can someone provide me with suggested code (I use ASP/VBScript) that would parse through the character string and save the six values for adding to a table?  (No need to provide database connectivity or interaction code - I can do that part)  
Mahalo plenty!
###My Test Survey,9/17/2011,10:57:09,Question2_1,multipleresponse,read MyPaper (occasionally or as a subscriber)|#|wrote for MyPaper (articles or poetry)|#|wrote to MyPaper (letter to editor)|#|imaged for MyPaper (drawing, photos)|#|labored for MyPaper (volunteer staff/grunt doing layout, typesetting, distribution)|#|hung out at MyPaper (ate, used facilities, recreational activity...you know what we mean) ###My Test Survey,9/17/2011,10:57:22,Question3_1,essay,Testing, testing, testing. ###My Test Survey,9/17/2011,10:57:25,Question4_1,essay,Testing, testing, testing. ###My Test Survey,9/17/2011,10:57:27,Question5_1,essay,Testing, testing, testing. ###My Test Survey,9/17/2011,10:57:28,Question6_1,essay,Testing, testing, testing. ###My Test Survey,9/17/2011,10:57:30,Question7_1,essay,Testing, testing, testing. ###My Test Survey,9/17/2011,10:57:33,Question8_1,essay,Testing, testing, testing.

Open in new window

0
Comment
Question by:OGSan
  • 8
  • 5
13 Comments
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
Dim myFields()
Dim mySQL

myFields = Split(ResponseString, ",")

mySQL = "INSERT INTO myTable (myField1, myField2, myField3, myField4, myField5, myField6) VALUES ('" & myField(0) & "', '" & myField(1) & "', '" & myField(2) & "', '" & myField(3) & "', '" & myField(4) & "', '" & myField(5) & "')"

This is just the concept. You probably have key fields and such you'd want to add, or you may be using ADODB with a recordset and be setting field values that way, but the split and using the split array is the concept here.
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
Wow, so the array is auto-magically created when the Split occurs?  That's nifty.  I'll test this out and report back soonest.  Thank you!
0
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
If possible, I would recommend that you use something else besides a comma for your delimiter. Perhaps the pipe character (|). That is because a comma often is in a string itself.
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
Thanks for the suggestion, I'll see if I can get that changed.
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
Hi, dsacker - I used your instruction, but am getting an error on that line reading, "Type mismatch."
Here is what the code looks like leading up to your instruction:
 
vstrSurveyLine = Request.Form("strSurveyLine")
	if	vstrSurveyLine <> "" then

' D E B U G  - verify values passed from QM_ResultsFromQuiz.asp
'		response.Write("D E B U G  -  B G N" & "<br />")
'		response.Write("1. vstrSurveyLine 	= "  & vstrSurveyLine 	& "<br />")
'		response.Write("D E B U G  -  E N D" & "<br />")

		' Parse string and place elements into an array
		dim myFields()
		myFields = Split(vstrSurveyLine, "|")
' D E B U G
		response.Write("D E B U G  -  B G N" & "<br />")
		response.write("parse and split response string <br />")
		response.write(myFields.value(0)& "<br />")
		response.write(myFields.value(1)& "<br />")
		response.write(myFields.value(2)& "<br />")
		response.write(myFields.value(3)& "<br />")
		response.write(myFields.value(4)& "<br />")
		response.write(myFields.value(5)& "<br />")
		response.write(myFields.value(6)& "<br />")
		response.Write("D E B U G  -  E N D" & "<br />")

Open in new window

0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
Error references line 11 above
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:OGSan
Comment Utility
...and I was able to change the delimiter character from commas to pipes.
0
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
Did you Dim the myFields?
0
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
Comment Utility
Also, besides the Dim myFields(), you may need to Set myFields = Split ...

Play with that, too (going on memory).
0
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
I forget sometimes this is classic ASP. With ASP.NET (and VB.NET) you don't do the "Set" command.
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
I Dim'd myFields()...and tried using Set myFields = Split...but same error occurs on that instruction.
Shucks.
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
I inserted () after myFields() = Split...and now I got a "Subscript out of range" error.  That's progress, I guess...!  :-)
0
 
LVL 1

Author Comment

by:OGSan
Comment Utility
I dropped the use of () and just used myFields - and I got passed that Split instruction...yeehaw!  Now I'm getting something else - but I'm awarding points anyway.
Thanks for tossing out enough clues to get me going again, dsacker.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now