Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11

Discussione: funzioni array

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    583

    funzioni array

    Ciao a tutti,

    dove posso trovare una lista delle operazioni/funzioni base che posso compiere su un array, tipo aggiungere elementi, eliminarne, modificare la dimensione?

    In modo particolare poi dovrei crearmi una funzione che mi elimini gli elementi doppi in un array, qualche suggerimento?

    Grazie

  2. #2
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    codice:
    Function InString(StringList,Value,Ch,cmpMode)
    	InString = InStr(1,Ch & StringList & Ch,Ch & Value & Ch,cmpMode)>0
    End Function
    
    Function EliminaDupplicati(byRef Ar,CaseSensitive)
    	Dim StringBuffer,Sep,cmpMode,Elm
    	Sep="@@"
    	cmpMode=1
    	If CaseSensitive then cmpMode=0
    	For Each Elm in Ar
    		If Not InString(StringBuffer,Elm,Sep,cmpMode) then StringBuffer = StringBuffer & Sep & Elm
    	Next
    	If StringBuffer<>"" then
    		StringBuffer = Mid(StringBuffer,Len(Sep)+1)
    		Erase Ar
    		Ar = Split(StringBuffer,Sep)
    	End if
    End Function
    Questo è un esempio:
    codice:
    Ar = Array("ELM1","Elm2","Elm3","Elm1")
    EliminaDupplicati Ar,False
    For Each Elm in Ar
    	Response.Write Elm & "
    "
    Next
    Se sul secondo parametro metti true (imposti il CaseSensitive), vedrai che Elm1 non verrà cancellato

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    583
    Grazie, davvero notevole.

  4. #4
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    Chiaramente il sistema funziona a condizione che la variabile Sep non sia mai contenuta negli elementi degli array

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    583
    Certo certo.

    Esistono in Asp (Vbscript) delle funzioni simili a quelle push e pop di javascript che inseriscono elementi in un'array o ne estraggono?

    In caso contrario come posso fare?

    Grazie

  6. #6
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    No. Gli array in vbscript non hanno pop e push.
    Hai 3 possibilità

    1.Crei una classe simile a quella di javascript implementando i metodi pop e push.
    2.Usi gli array di javascript in vbscript
    3.Usi gli array dinamici di Vbscript (Redim Preserve)

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    583
    Ho capito, grazie mille.

  8. #8
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    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
    %>

  9. #9
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    583
    Non potevo chiedere di più.

    Grazie un milione.

  10. #10
    Uppo perchè qualche giorno fa ho notato questa classe bellissima, ma ho trovato un bug.

    Provate questo

    codice:
    <%
    class arraymanip
    	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
    End class
    
    mioarray = split("a,b,c,d", ",")
    
    set am = new arraymanip
    
    stringa = "e,f,g,h"
    am.push mioarray, stringa
    
    for i=0 to ubound(mioarray)
    	response.write mioarray(i) & "
    "
    next
    %>
    il nuovo array non conterrà h. Bisogna mettere un +1 su itemcount, quindi!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.