Saturday, January 10, 2009

Array Merge

Did you ever have two arrays that you needed to merge together? If you were programming in PHP, you used the array_merge() function. If you were programming in ASP, you had to write a whole bunch of code because there is no array_merge() function... until now.


ASP

  1. function array_merge(byVal firstArray, byVal secondArray)
  2.     dim totalSize
  3.     dim i
  4.     dim combinedArray
  5.     ' Ensure that we're dealing with arrays.
  6.     if not isArray(firstArray) then
  7.         firstArray = Array(firstArray)
  8.     end if
  9.     if not isArray(secondArray) then
  10.         secondArray = Array(secondArray)
  11.     end if
  12.     ' Set up the new array.
  13.     totalSize = uBound(firstArray) + uBound(secondArray) + 1
  14.     combinedArray = firstArray
  15.     redim preserve combinedArray(totalSize)
  16.     for i = 0 to uBound(secondArray)
  17.         combinedArray(uBound(firstArray) + 1 + i) = secondArray(i)
  18.     next
  19.     array_merge = combinedArray
  20. end function

View ASP implementation on Snipplr

No comments: