Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse a string

Status
Not open for further replies.

jasonhuibers

Programmer
Joined
Sep 12, 2005
Messages
290
Location
CA
I have a string and would like to parse the values from the Spot variables in the correct sequence..

str = UserID=Jesse&Street=DuncanST&spot3=1&spot1=-1&spot4=4&hidName=&spot5=2&spot2=-1


Display like this:

-1 (Spot1)
-1 (Spot2)
1 (Spot3)
4 (Spot4)
2 (Spot5)
 
if it is a querystring you can simply do

Request.QueryString("spot1")
Request.QueryString("spot2")
... and so on...

-DNG

 
Actually it is not a url and the number of Spot variables is only determined at run time..
 
try this simple code snippet...

Code:
<%
Dim blah, str

str = "UserID=Jesse&Street=DuncanST&spot3=1&spot1=-1&spot4=4&hidName=&spot5=2&spot2=-1"

blah = split(str,"&")
for i=0 to Ubound(blah)
response.write blah(i) & "<br>"
next
%>

let me know if you can take it from there...the above code just gives you an idea...

-DNG
 
Only thing is I want to take only the values of the Spot variables and display in the correct asc sequence. Even though in the string they appear as Spot3, Spot1, Spot4, Spot5, Spot2 - I would like to have them display like:

Ie: (without the variable name apperaing)
-1 (Spot1)
-1 (Spot2)
1 (Spot3)
4 (Spot4)
2 (Spot5)
 
Did you actually try the code snippit? If so were you unable to extrapolate a solution?
 
Yes the code snippet displayed all variables followed by a carriage return. I need to have only the variables with Spot and the issue is in the sequence
 
ok one step further...let me know if you can take it from there...

Code:
<%
Dim blah, str, blahblah

str = "UserID=Jesse&Street=DuncanST&spot3=1&spot1=-1&spot4=4&hidName=&spot5=2&spot2=-1"

blah = split(str,"&")
for i=0 to Ubound(blah)


if Left(blah(i),4)="spot" then
blahblah= Mid(blah(i),5,1)

response.write blahblah

if instr(blah(i),"-") > 0 then
response.write Right(blah(i),2) & "<br>"
else
response.write Right(blah(i),1) & "<br>"
end if

end if
next
%>

you can create a two dimensional array and fill the values that i showed as display in the above code...and then you can arrange the values in order...

-DNG
 
This works great... thank you..

How can I sort an array, never done it before!
 
no problem...as i said i was just giving you an idea of how to deal with a string like that...

read more on asp/vbscript arrays online and then you can easily come up with the solution...here is one link that nmight help you...


just google for more links...

-DNG
 
I will give it a try thanks DotNetGnat!!! Very much appreciated
 
glad to help..post back if you cannot get it work...but give it a try...worth learning about arrays...

-DNG
 
an alternative to sorting your array:

Code:
<%@LANGUAGE=VBScript%>
<% Option Explicit %>
<%

dim sStr : sStr = "UserID=Jesse&Street=DuncanST&spot3=1&spot1=-1&spot4=4&hidName=&spot5=2&spot2=-1"

dim aParams : aParams = split(sStr, "&")
dim aKeyValue, i, sIDX
dim iIntMax : iIntMax = 0
dim sKeyword : sKeyword = "spot"
dim dList : set dList = Server.CreateObject("Scripting.Dictionary")

'/// Collect the Values wanted into the dictionary object
for i=lbound(aParams) to ubound(aParams)
	aKeyValue = split(aParams(i),"=")
	if left(aKeyValue(lbound(aKeyValue)), len(sKeyword)) = sKeyword then
		sIDX = mid(aKeyValue(lbound(aKeyValue)), len(sKeyword)+1)
		if cint(sIDX) > iIntMax then iIntMax = cint(sIDX) end if
		dList.add sIDX, aKeyValue(ubound(aKeyValue))
	end if
next

'/// Now cycle through the collection and output any that have values
for i = 0 to iIntMax
	if dList.Exists(cstr(i)) then
		response.write(i & ":" & dList.Item(cstr(i)) & "<br/>")
	end if
next

'/// Clean up
set dList = nothing

%>

A smile is worth a thousand kind words. So smile, it's easy! :-)
 

Cheers DNG.

Shuibers,

You may still want to look into Arrays and Array sorting - as it can be a very useful bit of knowledge. However be careful to choose the correct sorting method - many sorting methods are darn slow in comparison with the better ones.

My example above is a quick and dirty approach for this particular requirement, you can make it more generic if you used array sorting.



A smile is worth a thousand kind words. So smile, it's easy! :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top