Reverse Array

I need a function that can reserve an array in classic asp.


Thanks
MadDog986Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
thefritterfatboyConnect With a Mentor Commented:
Why would anyone WANT to reverse an array?

Why not just scroll through it backwards when you need it reversing?

For x = Ubound(array_name) to 0 step - 1
' do your array stuff
next
0
 
hongjunCommented:
try this simple example


<%
Dim intArray(3)
Dim tempArray()
Dim i, j

' initialize
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3

j = 0
For i = UBound(intArray) To 0 Step -1
    ReDim Preserve tempArray(j)
    tempArray(j) = intArray(i)
    j = j + 1
Next

For i = 0 To j - 1
    intArray(i) = tempArray(i)
Next

Erase tempArray

' now we got it in reverse
For i = 0 To UBound(intArray)
    Response.Write intArray(i) & "<br />"
Next
%>



hongjun
0
 
rdrunnerCommented:
Why reverse it really? You can also count down to access the elements and this way you wouldnt have to reverse it.

Anyway here is the code to do it...

dim eTmp
dim i
dim j (100)

for i = 0 to ubound(j)\2
      eTmp = j(ubound(j) - i)
      j(ubound(j) - i) = j(i)
      j(i) = etmp
next
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
rdrunnerCommented:
Hmmm let me comment some points about the 1st sample...

The 1st sample will waste a lot of cpu time since its doing "useless" work. Let me explain...

1. It uses a new array to reverse the other one and its not even passing the new array back. For huge arrays this could be a memory issue sine an asp page can run multiple times.
2. The array is redimmed for every element in the source array. if you want to use 2 arrays then you should redim the target array outside of the loop.


Dim intArray(3)
Dim tempArray()
Dim i, j

' initialize
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3

j = 0
For i = UBound(intArray) To 0 Step -1
    ReDim Preserve tempArray(j)
    tempArray(j) = intArray(i)
    j = j + 1
Next
0
 
hongjunCommented:
i am trying to really reverse the original array contents.
0
 
rdrunnerCommented:
Yes...

But the redimming an array for every element you add is evil. It will put some strain on the memory since to redim VB needs to create a new array and copy all elements into it and then dispose the old array. And if you do this for every element it adds up quite fast.... And you dont need a whole array in order to reverse one. exchanging the values of the first and last element will do so this would eliminate the need for a 2nd array. You only need 1 variable to hold 1 value while you reverse the array.

If you want to test the create and destroy memory scenario you can create a string and add "1" to it for about a million times.

cTest = cTest & "1"

You will SEE that this gets slower at the end since for every letter added vb needs to destroy and recreate the string
0
 
hongjunCommented:
Ya..the Big-O is increasing :(

hongjun
0
 
hongjunCommented:
this is lot better

<%
Dim intArray(3)
Dim tempString
Dim i
Dim str

' initialize
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3

tempString = ""
For i = UBound(intArray) To 0 Step -1
    tempString = tempString & intArray(i) & "-"
Next
If tempString <> "" Then
    tempString = Left(tempString, Len(tempString) - 1)
End If

i = 0
For Each str In Split(tempString, "-")
    intArray(i) = str
    i = i + 1
Next

For i = 0 To UBound(intArray)
    Response.Write intArray(i) & "<br />"
Next
%>



hongjun
0
 
hongjunCommented:
or this


<%
Dim intArray(3)
Dim temp
Dim i, j
Dim str

' initialize
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3

Set temp = CreateObject("Scripting.Dictionary")

i = 0
j = UBound(intArray)
Do While j >= 0
    temp.Add j, intArray(i)
    i = i + 1
    j = j - 1
Loop

For i = 0 To UBound(intArray)
    intArray(i) = temp(i)
Next

Set temp = Nothing

For i = 0 To UBound(intArray)
    Response.Write intArray(i)
Next
%>



hongjun
0
 
thefritterfatboyCommented:
I figure that idea has already been covered above, actually.... just with a lot more of other things involved in hongjun's post.
0
 
rdrunnerCommented:
Both of your improved versions still have the problem i mentioned. They both use more space then needed.

Option 1:

By creating and adding to a string you do ecactly the same as with redim preserve... A String is an array of bytes internally. So you have only changed the look here... Also you still need to allocate the space for all elements of the array to store them + 1 byte for the seperator (Which is also possible as an array elements for example -1 which could mess up your whole logic) But the amount of memory alocated is sum(1..n * (sizeof( arrayitem)+1)

Option 2:

You still need the memory to hold the whole array twice. But now you use a dictionary. I havent tested this but since you also need more variable assignments  ( 2 N assignments for an array of N elements... Its linear and thats good) I dont know how a dictionary will react to a large number of entries memory wise but i think it shoule be slower also.

My option:
Only needs to allocate sizeof( arrayitem) bytes to hold one pointer and will be done after 1.5 * N value assignments

0
 
thefritterfatboyCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasp/html/asptips.asp

Tips 13 + 20 highlight what rdrunner is trying to say. Listen to what he is saying regarding your ideas. Coding is getting your scripts to do what you need them to do, good coding is getting them to do it in the quickest and most resource-friendly way.
0
 
rdrunnerCommented:
Here is some statistics on the runtimes of the various ways...

The runtimes are for arrays with 1.000.000 records. The only exception is the string sort aproach which i decided to cap at 50 K records since it was busting my PC. These times are faster gathered on VB so the runtimes are even slower on ASP

End 1 - Sorting with only a Variable 0,157
End 3 - Sorting with a Dictionary 1,8441
End 1 - Sorting with recreation of the target array 8,782
End 1 - Sorting with a String (only 50!!! K records) 80,719
0
 
hongjunCommented:
:)
0
 
rdrunnerCommented:
Why didnt i get the credit?

Look at my 1st comment in this thread...

"Why reverse it really? You can also count down to access the elements and this way you wouldnt have to reverse it."

And after that i posted the code to reverse it :)
0
 
hongjunCommented:
yes, i agree rdrunner should get the credit here since the accepted answer was commented by rdrunner before too!

hongjun
0
 
hongjunCommented:
@rdrunner,

Perhaps you may wish to post a question in Community Support to report.

hongjun
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.