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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split and place in multidimensional array

Status
Not open for further replies.

magmo

Programmer
May 26, 2004
291
SE
Hi


I have a a form with x number of hidden fields that each have a value like this....

value="variable0|variable1|variable2|variable3"

So each value has a | as a delimeter. What I would like to do is to place each hidden fields value in a multi dimensional array and later access each array item and its value.

Could someone please show me how this could be achive?


Regards
 
Just assign array to each element of an array.
[tt]
<html>
<head>
<script language="vbscript">
dim adata() : redim adata(-1)
sub setit
redim adata(-1) 'reset all
set celem=document.forms(0).getElementsByTagName("input")
for i=0 to celem.length-1
if celem(i).type="hidden" then
redim preserve adata(ubound(adata)+1)
adata(ubound(adata))=split(celem(i).value,"|")
end if
next
set celem=nothing
[green]'this is for illustration, make sure within range
msgbox adata(1)(0) & vbcrlf & adata(0)(2)[/green]
end sub
</script>
</head>
<body>
<form name="frmtest">
<input type="text" value="a|b|c|d|e|f" /><br />
<input type="hidden" value="a|b|c|d|e|f" /><br />
<input type="text" value="g|h|i|j|k" /><br />
<input type="hidden" value="g|h|i|j|k" /><br />
</form>
<button onclick="setit">set the array</button>
</body>
</html>
[/tt]
 
Hi


Thanks alot for the code, it seem to be doing what it should, before you made your post I got this far....

Code:
Dim iLoop, iCounter, sString
dim endArray()  
iCounter = Request.Form("Count")'This is the number of hidden values seperated by "|"
For iLoop = 0 to iCounter
 sString = Request.Form(iLoop & ".PID")'this is the x number of hidden values seperated by "|"
 tmpArray = split(sString,"|")

 endArray(iLoop,0) = tmpArray(0)
 endArray(iLoop,1) = tmpArray(1) 'I get an error here "wrong matris index
 endArray(iLoop,2) = tmpArray(2)
 endArray(iLoop,3) = tmpArray(3)
Next
	
For i = LBound(endArray) to UBound(endArray,2)-1
 Response.Write endArray(0,i) & "<br>"
 Response.Write endArray(1,i) & "<br>"
 Response.Write endArray(2,i) & "<br>"
 Response.Write endArray(3,i) & "<br>"
Next


But I get a "Microsoft VBScript (0x800A0009)" error. Do you see whats wrong with this code?



Regards
 
>For i = LBound(endArray) to UBound(endArray,2)-1
What is this? lbound without 2nd param?

>endArray(iLoop,1) = tmpArray(1) 'I get an error here "wrong matris index
You never set the dimension of the dynamic endArray. And also later, you switch over the dimensions!

If I stick to my approach, it is this. (You make it more robust, as it is not sure you get correct request.form() all the time.
[tt]
Dim iLoop, iCounter, sString
dim endArray()
iCounter = Request.Form("Count")'This is the number of hidden values seperated by "|"
'[red]if icounter is 0-based, else icounter-1[/red]
redim endArray(icounter)
For iLoop = 0 to iCounter
sString = Request.Form(iLoop & ".PID")'this is the x number of hidden values seperated by "|"
endArray(iLoop) = split(sString,"|")
Next

For i = 0 to ubound(endArray)
Response.Write endArray(i)(0) & "<br>"
Response.Write endArray(i)(1) & "<br>"
Response.Write endArray(i)(2) & "<br>"
Response.Write endArray(i)(3) & "<br>"
Next
[/tt]
 
Hi

It worked like a charm, Thanks a million!


Best Regards


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top