DmitriF
04-11-2008, 10:14 AM
For those of you used to PHP, heres a function that mimics print_r. Doesnt quite
do multiple dimensions, but recursively visits nested arrays. Useful for debugging array values. Thought this might help someone out.
Usage: call print_r(arr3, 0)
Code:
public sub print_r(arr, depth)
if isArray(arr) then
If depth=0 then Response.Write ("<pre>" & Tab(depth)) end if
Response.Write ("Array <br />")
Response.Write (Tab(depth) & "(<br />")
for x=0 to uBound(arr)
if isArray(arr(x)) then
Response.write(Tab(depth+1) & "["&x&"] =>")
call print_r(arr(x), depth+2)
else
Response.write(Tab(depth+1) & "["&x&"] =>" & arr(x))
end if
Response.Write ("<br />")
next
Response.Write (Tab(depth) & ")")
If depth=0 then Response.Write ("</pre>") end if
end if
end sub
public function Tab(spaces)
val = ""
for x=1 to spaces
val=val & " "
next
Tab = val
end function
Example:
dim arr1: arr1 = Array(1, 2, 3, 4, Array(4, 1, 4, 6))
dim arr2: arr2 = Array(5, 6, 7, 8)
dim arr3: arr3 = Combine(arr1, arr2)
call print_r(arr3, 0)
Output:
Array
(
[0] =>1
[1] =>2
[2] =>3
[3] =>4
[4] =>Array
(
[0] =>4
[1] =>1
[2] =>4
[3] =>6
)
[5] =>5
[6] =>6
[7] =>7
[8] =>8
)
do multiple dimensions, but recursively visits nested arrays. Useful for debugging array values. Thought this might help someone out.
Usage: call print_r(arr3, 0)
Code:
public sub print_r(arr, depth)
if isArray(arr) then
If depth=0 then Response.Write ("<pre>" & Tab(depth)) end if
Response.Write ("Array <br />")
Response.Write (Tab(depth) & "(<br />")
for x=0 to uBound(arr)
if isArray(arr(x)) then
Response.write(Tab(depth+1) & "["&x&"] =>")
call print_r(arr(x), depth+2)
else
Response.write(Tab(depth+1) & "["&x&"] =>" & arr(x))
end if
Response.Write ("<br />")
next
Response.Write (Tab(depth) & ")")
If depth=0 then Response.Write ("</pre>") end if
end if
end sub
public function Tab(spaces)
val = ""
for x=1 to spaces
val=val & " "
next
Tab = val
end function
Example:
dim arr1: arr1 = Array(1, 2, 3, 4, Array(4, 1, 4, 6))
dim arr2: arr2 = Array(5, 6, 7, 8)
dim arr3: arr3 = Combine(arr1, arr2)
call print_r(arr3, 0)
Output:
Array
(
[0] =>1
[1] =>2
[2] =>3
[3] =>4
[4] =>Array
(
[0] =>4
[1] =>1
[2] =>4
[3] =>6
)
[5] =>5
[6] =>6
[7] =>7
[8] =>8
)