|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Array Sort
OK Shadow...this sorting that I've been at for the last two days !....
After a lot of 'my own' coding, looking at the problem from scratch and trying to code it, I've given up on my own code. The senario is ..I have two arrays (which I could make a single multidimensional array I suppose), but anyway sticking to my two arrays, each array entry is linked, i.e. array1(1) must always 'go along with' array2(1), and in array1 there is a list of names..in array2 there are numbers....array1 can have multiple entries of the same name, with different numbers in the same locations in array2...I have finally found the code below which deletes the multiple names from array1, but now i have to figure out how to add the numbers associated with the multiple same names in array1 from array2 and put the result in the same place in array2 as the name left in array1 which had multiple entries, and delete the entries from array2 which are not now associated with their partners in array1 as they have been deleted....maybe thats not very clear , and after writing this I can see a multidimensional array is what I need.... eg array1 array2 jointing 94.56 aerial 8 react 10.51 react 56 poling 13.41 would end up array1 array2 jointing 94.56 aerial 8 react 66.51 poling 13.41 dim retvalues() redim preserve retvalues(1) retvalues(0)=SkillType(0) For I=0 to (UBound(SkillType)-1) dim result result=0 For y=0 to (ubound(retvalues)-1) result=StrComp(SkillType(I),retvalues(y),1) if (result=0) then exit for end if Next if(result<>0)then redim preserve retvalues(ubound(retvalues)+1) retvalues(ubound(retvalues)-1)=SkillType(I) end if Next |
|
#2
|
||||
|
||||
|
the Dictionary object would be perfect for your case. using this, there is quite simple code to merge the arrays:
Code:
<%
Function MergeArrays(arr1, arr2, ByRef objDic)
'this function fills the given dictionary object with the unique items from arr1 as keys and the sum of associated items from arr2 as value.
'both arrays must be in same size.
'returns Empty if successful or error message on failure.
'verify same size:
If Not(UBound(arr1)=UBound(arr2)) Then
MergeArrays="Error: arrays are not of the same size. first array has "&(UBound(arr1)+1)&" items and second array has "&(UBound(arr2)+1)&" items."
Exit Function
End If
Dim x
Set objDic=Server.CreateObject("Scripting.Dictionary")
'loop over arrays:
For x=0 To UBound(arr1)
'verify numeric value in second array:
If Not(IsNumeric(arr2(x))) Then
objDic.RemoveAll
Set objDic=Nothing
MergeArrays="Error: found invalid item in second array: "&arr2(x)
Exit Function
End If
'apply second array value into dictionary:
If objDic.Exists(LCase(arr1(x))) Then
objDic(LCase(arr1(x))) = objDic(LCase(arr1(x)))+CDbl(arr2(x))
Else
objDic(arr1(x))=CDbl(arr2(x))
End If
Next
End Function
'usage:
Dim array1, array2, myDic
Dim strErrorMsg, key
array1=Array("jointing", "aerial", "react", "react", "poling")
array2=Array(94.56, 8, 10.51, 56, 13.41)
strErrorMsg=MergeArrays(array1, array2, myDic)
If Len(strErrorMsg)>0 Then
Response.Write("error while merging arrays: <br />"&strErrorMsg&"<br />")
Else
Response.Write("merged arrays:<br />")
For Each key In myDic.Keys
Response.Write(key&" - "&myDic(key)&"<br />")
Next
End If
%>
|
|
#3
|
|||
|
|||
|
Array sort
Excellent Shadow.......works a treat...i just changed a few things to make it work in my live enviornment...so thats that prob out of the way..onto the next.....will now continue coding some other pages before i need help again (I think) ....many thanks for the code....cul....>>>
|
|
#4
|
||||
|
||||
|
no problem... add another drink to the stash!
![]() |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Array Sort |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|