print_r function for classic ASP with vbscript

Apr 17

If you've done a bit of work using PHP you'll be familiar with the ever useful print_r function to output to screen complex datatypes like arrays or classes. If like me you're also still working on some legacy sites with classic ASP you'll regularly wish such a function exists in vbscript.

Sadly it doesn't.....but I wrote one which you're welcome to......

...well I say I wrote one...actually I had to write 4 functions and it still won't support classes because vbscript doesn't support variable introspection, but its better than a poke in the eye. So here you go:

  1. function print_r(data, dumpRef)
  2. dim dumpData
  3. if isArray(data) or cbool(instr(TypeName(data),"Dictionary")) or TypeName(data) = "ISessionObject" then
  4. dumpData = dump(data, 0)
  5. else
  6. if TypeName(data) = "Recordset" then
  7. dumpData = dumpQuery(data)
  8. else
  9. dumpData = TypeName(data) & ": " & data
  10. end if
  11. end if
  12. dumpData = "----------- DUMP CALLED " & dumpRef &" -------------" & vbcrlf & dumpData
  13. globalDumpData = globalDumpData & vbcrlf & dumpData
  14. if left(dumpRef,5) = "<pre:" then
  15. response.write "<pre>"&dumpData&"</pre>"
  16. if dumpRef = "<pre:stop>" then
  17. response.end
  18. end if
  19. end if
  20. print_r = dumpData
  21. end function
  22.  
  23. function dumpQuery(recordset)
  24. dim col, header, data, wrapper, q
  25. wrapper = "<table border=""1"">"&vbcrlf
  26. set q = recordset
  27. q.movefirst
  28. if q.absoluteposition = 1 then
  29. header = " <tr>" & vbcrlf
  30. for each col in q.fields
  31. header = header & " <th align=""left"" valign=""top"">"&col.name&"</th>" & vbcrlf
  32. next
  33. header = header & " </tr>" & vbcrlf
  34. q.movefirst
  35. end if
  36.  
  37. data = rs.GetString(2, q.recordcount+1, "</td>" & vbcrlf & " <td valign=""top"">", "[#]", "")
  38. data = left(data, len(data) - 3)
  39. data = replace(data, "[#]", "</td>" & vbcrlf & " </tr>" & vbcrlf & " <tr>" & vbcrlf & " <td valign=""top"">")
  40. data = " <tr>" & vbcrlf & " <td valign=""top"">" & data & "</td>" & vbcrlf & " </tr>"
  41. wrapper = wrapper & header & data & vbcrlf & "</table>"
  42. q.movefirst
  43. dumpQuery = wrapper
  44. end function
  45.  
  46. function dump(data, depth)
  47. dim output, x
  48. if isArray(data) then
  49. output = "Array <br />"
  50. output = output & Tab(depth) & "(<br />"
  51. for x=0 to uBound(data)
  52. output = output & Tab(depth+1) & "["&x&"] => "
  53. output = output & dump(data(x), depth+2)
  54. output = output & "<br />"
  55. next
  56. output = output & Tab(depth) & ")"
  57. elseif cbool(instr(TypeName(data),"Dictionary")) then
  58. output = TypeName(data) & " <br />"
  59. output = output & Tab(depth) & "(<br />"
  60. for each x in data
  61. output = output & Tab(depth+1) & "["&x&"] => "
  62. output = output & dump(data(x), depth+2)
  63. output = output & "<br />"
  64. next
  65. output = output & Tab(depth) & ")"
  66. elseif TypeName(data) = "ISessionObject" then
  67. output = TypeName(data) & "<br />(<br/>"& Tab(depth+1) & "Contents<br />"
  68. output = output & Tab(depth+1) & "(<br />"
  69. for each x in data.contents
  70. output = output & Tab(depth+2) & "["&x&"] => "
  71. output = output & dump(data(x), depth+2)
  72. output = output & "<br />"
  73. next
  74. output = output & Tab(depth+1) & ")<br/><br/>"
  75. output = output & Tab(depth+1) & "StaticObjects<br />"
  76. output = output & Tab(depth+1) & "(<br />"
  77. for each x in data.StaticObjects
  78. output = output & Tab(depth+2) & "["&x&"] => "
  79. output = output & dump(data(x), depth+2)
  80. output = output & "<br />"
  81. next
  82. output = output & Tab(depth+1) & ")<br/>"
  83. output = output & Tab(depth) & ")"
  84. elseif TypeName(data) = "Recordset" then
  85. output = output & dumpQuery(data)
  86. output = output & "<br />"
  87. else
  88. output = output & data
  89. end if
  90. dump = output
  91. end function
  92.  
  93. public function Tab(spaces)
  94. dim val, x
  95. val = ""
  96. for x=1 to spaces
  97. val = val & " "
  98. next
  99. Tab = val
  100. end function

You can then use it like this:

  1. print_r request.cookies(), "[pre:]#cookies"
  2. print_r array("test", "me", "please"), "[pre:]array"
  3. dim x
  4. x = print_r("HELLO", "")
  5. response.write x

 

Tags: 
ASP
tips

Recent Comments

Browse by date

Join us on Facebook