Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 3.50 average. Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 2nd, 2005, 02:10 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
Post Classic ASP and vbscript ArrayList Version 1.0

ever since I met ArrayList in .NET, I can't get it out of my thoughts. it was love from first sight. as classic ASP does not have this wonderful component, I decided I would add it.
Code:
<% Option Explicit %>
<%
Class ArrayList
	Private CAPACITY
	Private m_arrData()
	Private m_Count
	Private m_DefaultComparer
	Private Sub Class_Initialize
		CAPACITY=100
		Redim m_arrData(CAPACITY-1)
		m_Count=0
		m_DefaultComparer=""
	End Sub
	
	Public Property Get Count
		Count=m_Count
	End Property
	
	Public Property Get DefaultComparer
		DefaultComparer=m_DefaultComparer
	End Property
	
	Public Property Let DefaultComparer(Value)
		m_DefaultComparer=Value
	End Property

	Public Sub Add(value)
		If m_Count>UBound(m_arrData) Then
			Call MakeMorePlace
		End If
		If IsObject(value) Then
			Set m_arrData(m_Count)=value
		Else  
			m_arrData(m_Count)=value
		End If
		m_Count=m_Count+1
	End Sub
	
	Public Sub Remove(value)
		Dim x
		For x=0 To m_Count-1
			If Compare(m_arrData(x), value, m_DefaultComparer)=0 Then
				m_arrData(x)=Empty
			End If
		Next
		Call RemoveEmptyItems
	End Sub
	
	Public Default Property Get Item(index)
		If Not(IsNumeric(index)) Then
			Err.Raise 15000, "ArrayList", "invalid parameter given to Item: not numeric"
		End If
		If (index<0) Or (index>=m_Count) Then
			Err.Raise 15001, "ArrayList", "index out of range: ("&index&") can't be less than zero or greater than count"
		End If
		If IsObject(m_arrData(index)) Then
			Set Item=m_arrData(index)
		Else  
			Item=m_arrData(index)
		End If
	End Property
	
	Public Function ToArray()
		Dim result(), x
		If m_Count=0 Then
			ToArray=Array() 'empty array
			Exit Function
		End If
		ReDim result(m_Count-1)
		For x=0 To m_Count-1
			If IsObject(m_arrData(x)) Then
				Set result(x)=m_arrData(x)
			Else  
				result(x)=m_arrData(x)
			End If
		Next
		ToArray=result
	End Function
	
	Public Sub Sort(strComparer)
		Dim x, y, maxValue
		Dim maxIndex, curValue, tempValue
		
		For x=0 To m_Count-1
			maxIndex=x
			If IsObject(m_arrData(x)) Then
				Set maxValue=m_arrData(x)
			Else  
				maxValue=m_arrData(x)
			End If
			For y=x+1 To m_Count-1
				If IsObject(m_arrData(y)) Then
					Set curValue=m_arrData(y)
				Else  
					curValue=m_arrData(y)
				End If
				If Compare(curValue, maxValue, strComparer)>0 Then
					If IsObject(curValue) Then
						Set maxValue=curValue
					Else  
						maxValue=curValue
					End If
					maxIndex=y
				End If
			Next
			If maxIndex<>x Then
				If IsObject(m_arrData(maxIndex)) Then
					Set tempValue=m_arrData(maxIndex)
				Else  
					tempValue=m_arrData(maxIndex)
				End If
				If IsObject(m_arrData(x)) Then
					Set m_arrData(maxIndex)=m_arrData(x)
				Else  
					m_arrData(maxIndex)=m_arrData(x)
				End If
				If IsObject(tempValue) Then
					Set m_arrData(x)=tempValue
				Else  
					m_arrData(x)=tempValue
				End If
			End If
		Next
	End Sub
	
	Private Sub MakeMorePlace
		ReDim Preserve m_arrData(UBound(m_arrData)+CAPACITY)
	End Sub
	
	Private Sub RemoveEmptyItems
		Dim blnContinue, x, delIndex
		blnContinue=True
		Do Until blnContinue=False
			delIndex=-1
			For x=0 To m_Count-1
				If IsEmpty(m_arrData(x)) Then
					delIndex=x
				End If
			Next
			If delIndex>=0 Then
				For x=delIndex To m_Count-2
					If IsObject(m_arrData(x+1)) Then
						Set m_arrData(x)=m_arrData(x+1)
					Else  
						m_arrData(x)=m_arrData(x+1)
					End If
				Next
				m_arrData(m_Count-1)=Empty
				m_Count=m_Count-1
			End If
			blnContinue=(delIndex >= 0)
		Loop
	End Sub
	
	Private Function Compare(item1, item2, strComparer)
		'look for custom comparer:
		If Len(strComparer)>0 Then
			On Error Resume Next
				Compare=Eval(strComparer&"(item1, item2)")
				If Err.Number<>0 Then
					On Error Goto 0
					Err.Raise 15010, "ArrayList", "Can't compare items: invalid comparer or error on custom comparison: "&Err.Description&" ("&strComparer&")"
				End If
			On Error Goto 0
			Exit Function
		End If
		
		'no default comparer, use built in comparison, if possible:
		If IsObject(item1) Or IsObject(item2) Then
			Err.Raise 15002, "ArrayList", "Can't compare items: no custom comparer is defined and complex items found."
		End If
		'If TypeName(item1)=TypeName(item2) Then
		If item1=item2 Then
			Compare=0
		Else  
			If item1>item2 Then
				Compare=1
			Else  
				Compare=-1
			End If
		End If
		'End If
	End Function

	Private Sub Class_Terminate
		Dim x
		For x=0 To UBound(m_arrData)
			If IsObject(m_arrData(x)) Then
				Set m_arrData(x)=Nothing
			End If
		Next
		Erase m_arrData
	End Sub
End Class

'must return zero if items are equal, positive number if item1 bigger then item2 and negative number if item2 is bigger than item1.
Function MyComparer(item1, item2)
	If TypeName(item1)="String" And TypeName(item2)="String" Then
		MyComparer=StrComp(item1, item2)
		Exit Function
	End If
	If TypeName(item1)="Integer" And TypeName(item2)="Integer" Then
		MyComparer=CLng(item1)-CLng(item2)
		Exit Function
	End If
	If TypeName(item1)="String" And TypeName(item2)="Integer" Then
		If IsNumeric(item1) Then
			MyComparer=CLng(item1)-CLng(item2)
			Exit Function
		End If
		MyComparer=1
		Exit Function
	End If
	If TypeName(item1)="Integer" And TypeName(item2)="String" Then
		If IsNumeric(item2) Then
			MyComparer=CLng(item1)-CLng(item2)
			Exit Function
		End If
		MyComparer=-1
		Exit Function
	End If
	MyComparer=0
End Function

Function NameComparer(file1, file2)
	NameComparer=StrComp(file1.Name, file2.Name, 1)
End Function

Function SizeComparer(file1, file2)
	SizeComparer=file1.Size-file2.Size
End Function

Function DateCreatedComparer(file1, file2)
	DateCreatedComparer=DateDiff("s", file2.DateCreated, file1.DateCreated)
End Function

Function DateLastModifiedComparer(file1, file2)
	DateLastModifiedComparer=DateDiff("s", file2.DateLastModified, file1.DateLastModified)
End Function

Function ExtensionComparer(file1, file2)
	Dim strExtension1, strExtension2, arrTemp
	arrTemp=Split(file1.Name, ".")
	strExtension1=arrTemp(UBound(arrTemp))
	Erase arrTemp
	arrTemp=Split(file2.Name, ".")
	strExtension2=arrTemp(UBound(arrTemp))
	Erase arrTemp
	ExtensionComparer=StrComp(strExtension1, strExtension2, 1)
End Function

'usage:
Dim myList
Set myList=New ArrayList
Call myList.Add(5)
Call myList.Add(8)
Call myList.Add("hello")
Response.Write "ordinary ArrayList with numbers and string, before sorting:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"
Call myList.Sort("MyComparer")
Response.Write "ordinary ArrayList with numbers and string, after sorting with custom comparer:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"
Call myList.Remove(8)
Response.Write "ordinary ArrayList with numbers and string, after the item 8 has been removed:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"

Dim FOLDER_PATH :FOLDER_PATH=Server.MapPath("images")
Dim filesList, objFSO, objFolder
Dim objFile, strData, x
Set filesList=New ArrayList
Set objFSO=Server.CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(FOLDER_PATH)
For Each objFile In objFolder.Files
	Call filesList.Add(objFile)
Next
Set objFolder=Nothing
Set objFSO=Nothing
strData="Contents of the folder "&FOLDER_PATH&", raw: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("NameComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by name: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("SizeComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by size: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&" ("&filesList(x).Size&" bytes)<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("DateCreatedComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by date created: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&" (created at: "&filesList(x).DateCreated&")<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("DateLastModifiedComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by date last modified: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&" (date last modified: "&filesList(x).DateLastModified&")<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("ExtensionComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by extension: <br />"
For x=0 To filesList.Count-1
	strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
%>

seriously now... this component is the most useful I know. very generic, very flexible and can make life lot easier.

quick guide for those unfamiliar with such component:
http://forums.aspfree.com/showpost....205&postcount=4
(due to 10k characters limit in single post, I can't have it all in the same post)

Happy Coding!
Attached Files
File Type: txt ArrayList.asp.txt (8.6 KB, 333 views)

Last edited by Shadow Wizard : July 9th, 2005 at 10:50 AM. Reason: new version

Reply With Quote
  #2  
Old July 2nd, 2005, 04:05 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,770 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 58 m 22 sec
Reputation Power: 469
What about adding a Sort method, to automatically sort the elements?

Reply With Quote
  #3  
Old July 2nd, 2005, 05:30 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
yeah, I mentioned this - intend to add this in the next version.
however, this will require the user (programmer using the code) to implement custom comparer - not going to be simple to explain such thing.

Reply With Quote
  #4  
Old July 9th, 2005, 10:49 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
Quick Guide:
1. have the Class code in seperate file called ArrayList.asp and include this file whenever you need to use ArrayList.
2. to create instance of the component, simply have this:
Code:
Set myList=New ArrayList

myList would be the ArrayList object.
3. to add items, have such syntax:
Code:
Call myList.Add("hello")

you can add whatever you want including objects of all kinds e.g. recordset however you won't be able to Remove such objects.
4. to remove items, have such code:
Code:
Call myList.Remove("hello")

this will remove all items equal to this.
5. to iterate over all items have such code:
Code:
 For x=0 To myList.Count-1
    Response.Write("item #"&(x+1)&": "&myList(x)&"<br />")
 Next

(this is actually shortcut for myList.Item(x))
6. to get single item at desired index:
Code:
myList(index)

7. to get ordinary one dimension array out of the ArrayList:
Code:
myArray=myList.ToArray()


--Added at July 9th, 2005:
Sort has been added, now the ArrayList can be sorted using custom comparer. The comparer is getting two items from the ArrayList and must return integer number. it must return 0 if the items are both the same, positive number if the first item is bigger than the second item and negative number if the second item is bigger than the first item. the return value itself (e.g. if it's 5 or 10) does not matter. this comparer is used when comparing items to find the biggest and smallest items when sorting.
Two basic examples has been added as well: one for list with basic type items (numbers and strings) and one for Files, where the ArrayList hold files from specific folder on the server. this demonstrates how simple it becomes to sort files using the ArrayList sort method - only 3 lines for most comparers.
---


that's it, for now... feel free to comment, suggest and point problems. this is the final version, I plan to add method AddRange that would get array of items and add those items to the ArrayList one by one, it would be useful for example when storing the ArrayList, as Array (using the ToArray() method), in the Session, then you have to build the ArrayList back in some other page.

Happy Coding!

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Classic ASP and vbscript ArrayList Version 1.0


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT