Splitting a string twice (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


JohnH -> Splitting a string twice (4/21/2008 13:03:39)

Hi all,

I have a string that looks like this:

183*22,193*3,150*10

Basically, the 183, 193 and 150 are product codes and the 22, 3 and 10 are quantities.

I want to be able to split the string out so that the product code and quantities are in seperate arrays.

I have managed to seperate individual product codes and quantities by using the following code.
	<%
	Dim basket
	Dim array
	basket = Request.Cookies("ShoppingCart")
	array = split(basket,",")
	For Each item In array
	Response.Write(item & "<br>")
	Next
	%>


How do I know split these out by detecting the *?

Any help would be greatfully appreciated.

Many thanks,

John




rdouglass -> RE: Splitting a string twice (4/21/2008 14:13:11)

How 'bout instead using a 2 dimensional array like this:

<%
Dim basket
Dim array
basket = Request.Cookies("ShoppingCart")
array = split(basket,",")

ReDim array2Dim(1,ubound(array))
For i = 0 To ubound(array)
array2Dim(0,i) = left(array(i),instr(array(i),"*")-1)
array2Dim(1,i) = mid(array(i),instr(array(i),"*")+1)
Next

'spit out what you got
For i = 0 To ubound(array2Dim,2)
Response.write(array2Dim(0,i) & "-" & array2Dim(1,i) & "<br />")
Next
%>


If you're comfortable with a single dimension array, maybe you're comfortable with a 2-D as well. I see this as being easier just grabbing each 'key:value' pair directly and finding each using:

instr(array(i),"*")

and then just add 1 or subtract 1 whether you're checking forward or backwards. Now there's another key part right here also:

ReDim array2Dim(1,ubound(array))

That is building a 2-D array based on the length of the first array.

Make sense? That's how I'd do it. Now we have them in a single 2-D array instead of being messing with several 1-D arrays. (In reality that's what a 2-D array is anyways.)

That help any?




JohnH -> RE: Splitting a string twice (4/22/2008 4:27:38)

Thanks rdouglas. Worked a treat.

Next question...

How can I now use the split arrays in a sql query? I want to be able to grab the product codes and use a SQL select command to find the product description and also multiply the quantities by the product price. I am ok with the SQL syntax, I just need some help in using the arrays. Tricky bit is that I won't know how many products will be selected.

Just to clarify, my original string was:

183*22,193*3,150*10

Thanks to your help, I know have the 183, 193 and 150 split out (these are product codes) and also the 22,3 and 10 are split (these are quantities). Taking the first product and quantity as an example I want to be able to do a sql query like the following:

select product_description, sum(product_price*22) as 'total price' from pricing where product_code='183'

so, the quantity 22 would be an array and so would the 183 and there could be a number of different product codes and quantities.

Hope that makes sense and you can help!

Thanks,

John




rdouglass -> RE: Splitting a string twice (4/22/2008 8:46:15)

quote:

select product_description, sum(product_price*22) as 'total price' from pricing where product_code='183'


If you have them in that 2-array, you should be able to do something like this:

FOR i = 0 To ubound(array2Dim,2)
mySQL = "select product_description, sum(product_price*" & array2Dim(1,i) & ") as 'total price' from pricing where product_code='" & array2Dim(0,i) & "'"
'execute the SQL here
Next

That should loop thru each row of array2Dim and build each SQL statement on-the-fly.

That what you're looking for?




JohnH -> RE: Splitting a string twice (4/23/2008 6:03:27)

That worked perfectly, thanks again rdouglas.

Can you help me again though. The original string now has another parameter.

It now consists of product_id,quantity,hours. An example of two records would be 187*2*10,193*5*12.

The code you gave me before is

<%
Dim basket
Dim array
basket = Request.Cookies("ShoppingCart")
array = split(basket,",")

ReDim array2Dim(1,ubound(array))
For i = 0 To ubound(array)
array2Dim(0,i) = left(array(i),instr(array(i),"*")-1)
array2Dim(1,i) = mid(array(i),instr(array(i),"*")+1)
Next

'spit out what you got
For i = 0 To ubound(array2Dim,2)

Response.write (array2Dim(1,i) & " "  & (array2Dim(1,i) & "<br />"

Next
%>


How do I split this out a third time to grab the new hours data?

Thanks in anticipation.

John




rdouglass -> RE: Splitting a string twice (4/23/2008 9:39:36)

quote:

ReDim array2Dim(1,ubound(array))
For i = 0 To ubound(array)
array2Dim(0,i) = left(array(i),instr(array(i),"*")-1)
array2Dim(1,i) = mid(array(i),instr(array(i),"*")+1)
Next


Just a little trickier and this time we should split it again. Something like this:

ReDim array2Dim(2,ubound(array))
For i = 0 To ubound(array)

tempArray = split(array(i),"*")
for j = 0 To 2 'or we could use ubound(tempArray)
array2Dim(j,i) = temparray(j)
Next


Next

That should be pretty close.




JohnH -> RE: Splitting a string twice (4/23/2008 9:43:13)

Thanks rdouglas, I am not sure where this code should be placed in the existing code you gave me - can you help?

Thanks,

John




rdouglass -> RE: Splitting a string twice (4/23/2008 10:12:40)

This code:

ReDim array2Dim(2,ubound(array)) 
For i = 0 To ubound(array) 
tempArray = split(array(i),"*") 
for j = 0 To 2 'or we could use ubound(tempArray) 
array2Dim(j,i) = temparray(j) 
Next 
Next 

should replace this code:
ReDim array2Dim(1,ubound(array))
For i = 0 To ubound(array)
array2Dim(0,i) = left(array(i),instr(array(i),"*")-1)
array2Dim(1,i) = mid(array(i),instr(array(i),"*")+1)
Next


You'd grab your values the same way except you'd add a 3rd item in there:

For i = 0 To ubound(array2Dim,2)
Response.write (array2Dim(1,i) & " " & array2Dim(1,i) & " " & array2Dim(2,i) & "<br />")
Next

See, a 2 dimensional array can be thought of like a spreadsheet. You access each cell by specifying the column number and rownumber like so:

array(column,row)

but being sure to remeber that arrays are "0-based" meaning the first cell is array(0,0) and not array(1,1)

That help any?




JohnH -> RE: Splitting a string twice (4/23/2008 10:49:11)

Perfect!!!

If you were in the UK I would buy you a beer!

Thanks for all your help rdouglas




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.046875