Guarda cosa ho trovato tra le directory di files asp... forse fa al caso tuo
codice:
class arraymanip
' useful array manipulation functions
' for vbscript arrays
public function pop(byref thearray)
' returns the last value in the
' array and removes it from the
' array, shortening the array
' by one element
pop = thearray(ubound(thearray))
redim preserve thearray(ubound(thearray) - 1)
end function
public function push(byref thearray, byval thedatatoappend)
' appends new elements to an
' array and returns the new
' length of the array (ubound)
dim itemcount, tmp, i, oldubound, j
oldubound = ubound(thearray)
itemcount = 0
tmp = split(thedatatoappend, ",")
itemcount = ubound(tmp)
redim preserve thearray(oldubound + itemcount)
i = 0
for j = oldubound + 1 to ubound(thearray)
thearray(j) = trim(tmp(i))
i = i + 1
next
push = ubound(thearray)
end function
public function shift(byref thearray)
' removes the first element of an array
' and displays it. Shifts every other element
' down one element and shortens the array by
' 1 element.
dim i
shift = thearray(lbound(thearray))
for i = 1 to ubound(thearray)
thearray(i - 1) = thearray(i)
next
redim preserve thearray(ubound(thearray) - 1)
end function
public function splice(byref thearray, byval start, byval deletecount, byval optionallist)
' removes elements from an array and
' optionally inserts new values to
' replace the deleted elements.
' Returns the removed elements as a
' new array.
dim i, j, newarray()
dim tmp, outputarray()
dim ct, arrayub
arrayub = ubound(thearray)
ct = 0 : j = 0
if (deletecount < 0) or _
(deletecount > arrayub) or _
(not isnumeric(deletecount)) then _
deletecount = arrayub
if (start < 0) or (not isnumeric(start)) then start = 0
if start > arrayub then _
start = arrayub
redim newarray(deletecount)
for i = start to (start + deletecount - 1)
newarray(j) = thearray(i)
thearray(i) = ""
j = j + 1
next
tmp = split(optionallist, ",")
j = start
for i = 0 to ubound(tmp)
thearray(j) = trim(tmp(i))
if j = (start + deletecount - 1) then
exit for
end if
j = j + 1
next
splice = newarray
for i = 0 to arrayub
thearray(i) = trim(thearray(i))
if len(thearray(i)) = 0 then
ct = ct + 1
end if
next
redim outputarray( arrayub - ct )
j = 0
for i = 0 to ubound(thearray)
if not len(trim(thearray(i))) = 0 then
outputarray(j) = thearray(i)
j = j + 1
end if
next
thearray = outputarray
end function
public function unshift(byref thearray, byval thedatatoprepend)
' returns an array with the specified
' elements added to the beginning of
' the original array
dim tmp, i, newarray()
dim j
tmp = split(thedatatoprepend, ",")
redim newarray(ubound(thearray) + ubound(tmp) + 1)
j = ubound(tmp) + 1
for i = 0 to ubound(thearray)
newarray(j + i) = thearray(i)
next
for i = 0 to ubound(tmp)
newarray(i) = trim(tmp(i))
next
unshift = newarray
thearray = newarray
end function
public function hasdups(byref thearray)
dim d, item, ber
ber = false
set d = createobject("scripting.dictionary")
on error resume next
for each item in thearray
d.add item, ""
if err then
ber = true
exit for
end if
next
on error goto 0
d.removeall
set d = nothing
hasdups = ber
end function
public function remdups(byref thearray)
dim d, item, ber, newarray()
dim i, a
i = 0
redim newarray(ubound(thearray))
ber = false
set d = createobject("scripting.dictionary")
on error resume next
for each item in thearray
d.add item, ""
next
on error goto 0
a = d.keys
d.removeall
set d = nothing
remdups = a
thearray = a
end function
public function revarray(byref arrayinput)
dim i, ubnd
dim newarray()
ubnd = ubound( arrayinput )
redim newarray(ubnd)
for i = 0 to ubound( arrayinput )
newarray( ubnd - i ) = arrayinput( i )
next
revarray = newarray
arrayinput = newarray
end function
public function sort(byref unsortedarray)
dim front, back, loc, temp, arrsize
arrsize = ubound(unsortedarray)
for front = 0 to arrsize - 1
loc = front
for back = front to arrsize
if unsortedarray(loc) > _
unsortedarray(back) then
loc = back
end if
next
temp = unsortedarray(loc)
unsortedarray(loc) = unsortedarray(front)
unsortedarray(front) = temp
next
sort = unsortedarray
end function
public function slice(byref thearray, byval start, byval theend)
' returns part of an array as a new array.
' doesn't modify the original array
dim lstart, lend, i, j, newarray()
lstart = lbound(thearray) : lend = ubound(thearray)
if start < lstart then start = 0
if start > lend then start = lend
if theend > lend then theend = lend
if theend < lstart then theend = 0
if theend = "" then theend = lend
redim preserve newarray(theend - start)
j = 0
for i = start to theend
newarray(j) = thearray(i) : j = j + 1
next
slice = newarray
end function
end class
function showarray(byval arr)
dim i
response.write "test array looks like this:
"
for i = 0 to ubound(arr)
response.write "" & arr(i) & "
"
next
response.write "
"
end function
Questo l'esempio:
codice:
<%
dim AM, myArray
' create an array to manipulate
myArray = Array("brian", "matt", "jay", "erik", "steve", "brian")
showarray myarray
' create an instance of the ArrayManip class object
set AM = new ArrayManip
' sort the array - ascending
' (the sort method returns the sorted array
' but also manipulates the input array
' directly so it's result can be discarded,
' like below...)
AM.Sort myArray
' myArray now looks like this:
' Array("brian", "brian", "erik", "jay", "matt", "steve")
showarray myarray
' sort the array - descending, by reversing the sorted array
AM.RevArray myArray
' myArray now looks like this:
' Array("steve", "matt", "jay", "erik", "brian", "brian")
showarray myarray
' check for duplicates and if any are found, remove them
If AM.HasDups(myArray) then AM.RemDups myArray
' myArray now looks like this:
' Array("steve", "matt", "jay", "erik", "brian")
showarray myarray
' use the unshift method to add 3 new elements to the
' beginning of the array
AM.UnShift myArray, "ralph, harold, jake"
' myArray now looks like this:
' Array("ralph", "harold", "jake", "steve", "matt", "jay", "erik", "brian")
showarray myarray
' release the class instance from memory
set AM = nothing
%>