Solved

Reverse Array

Posted on 2004-10-20
17
905 Views
Last Modified: 2012-06-21
I need a function that can reserve an array in classic asp.


Thanks
0
Comment
Question by:MadDog986
  • 8
  • 6
  • 3
17 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 12367186
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
 
LVL 11

Expert Comment

by:rdrunner
ID: 12367229
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
 
LVL 11

Expert Comment

by:rdrunner
ID: 12367268
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
 
LVL 33

Expert Comment

by:hongjun
ID: 12368193
i am trying to really reverse the original array contents.
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 12368269
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
 
LVL 33

Expert Comment

by:hongjun
ID: 12368280
Ya..the Big-O is increasing :(

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 12368302
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
 
LVL 33

Expert Comment

by:hongjun
ID: 12368317
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Accepted Solution

by:
thefritterfatboy earned 125 total points
ID: 12369834
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
 
LVL 10

Expert Comment

by:thefritterfatboy
ID: 12369850
I figure that idea has already been covered above, actually.... just with a lot more of other things involved in hongjun's post.
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 12370399
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
 
LVL 10

Expert Comment

by:thefritterfatboy
ID: 12370454
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
 
LVL 11

Expert Comment

by:rdrunner
ID: 12371494
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
 
LVL 33

Expert Comment

by:hongjun
ID: 12371559
:)
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 12419263
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
 
LVL 33

Expert Comment

by:hongjun
ID: 12419301
yes, i agree rdrunner should get the credit here since the accepted answer was commented by rdrunner before too!

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 12419306
@rdrunner,

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

hongjun
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now