![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Inserting or deleting elements in an array Is there a simple way to either insert elements into an array or to delete them, without copying the array to another? Efficiency is of no real concern in this application, as the arrays consist of under 50 elements and only one or two insertions or deletions are ever necessary. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Inserting or deleting elements in an array "JJ" <jim.mcatee@xxxxxx> wrote in message news:c2146673-86cb-4da3-bd1c-dfa6b69da6ab@xxxxxx Quote: > Is there a simple way to either insert elements into an array or to > delete them, without copying the array to another? Efficiency is of > no real concern in this application, as the arrays consist of under 50 > elements and only one or two insertions or deletions are ever > necessary. Dim and ReDim with the Preserve keyword. Using the Preserve keyword is not efficient. Can you use a dictionary object? This is an associative array of (key, item) pairs. Key values must be unique, but it is very efficient. The object has Add, Remove, and Exists methods. You can also easily convert to an array of either key values or item values. When I am keeping track of something, where I don't really need to assign anything for the item in the pair, I simply use True. For example, to keep track of groups: ======== Set objUser = GetObject("LDAP://cn=Jim Smith,ou=West,dc=MyDomain,dc=com") Set objGroupList = CreateObject("Scripting.Dictionary") objGroupList.CompareMode = vbTextCompare For Each objGroup In objUser.Groups If (objGroupList.Exists(objGroup.Name) = False) Then ' Add the group name to the dictionary object. ' The Key value is the group name, the item value is True. objGroupList.Add objGroup.Name, True End If Next If (objGroupList.Exists("cn=Domain Users") = True) Then objGroupList.Remove "cn=Domain Users" End If ' Convert to an array of group names. arrGroups = objGroupList.Keys ======= Check WSH documentation for details. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Inserting or deleting elements in an array Quote: > Is there a simple way to either insert elements into an array or to > delete them, without copying the array to another? Efficiency is of > no real concern in this application, as the arrays consist of under 50 > elements and only one or two insertions or deletions are ever > necessary. like this: Dim A() ReDim A(10) ReDim Preserve A(20) ' extends array without losing ' existing data. But you can't just drop an element from the array. There are two options I can think of. 1) Assign a value to the array member that will mark it as null: For i = 0 to UBound(A) If A(i) <> "-" then ' a dash here used to invalidate ' a specific array member. 2) Use a Dictionary instead. You could also use a Collection, but a Dictionary is more flexible and is said to be notably more efficient. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Inserting or deleting elements in an array "JJ" <jim.mcatee@xxxxxx> wrote in message news:c2146673-86cb-4da3-bd1c-dfa6b69da6ab@xxxxxx Quote: > Is there a simple way to either insert elements into an array or to > delete them, without copying the array to another? Efficiency is of > no real concern in this application, as the arrays consist of under 50 > elements and only one or two insertions or deletions are ever > necessary. new array. Sort of. Here's how: xxx is an array of 50 elements (0 through 49), and you want to delete element 17: for i = 17 to 48 xxx(i) = xxx(i+1) next redim xxx(48) To insert it back: for i = 49 to 18 step -1 xxx(i) = xxx(i-1) next redim xxx(49) Obviously, you'd develop a sub or function to do this, and use the actual length of the array instead of the literal 48 and 49. Of course, in reality, the redim statement *is* creating a new array. Regardless, you say that "efficiency" is not an issue. I agree that the choice of algorithm should not be based on "efficiency", as it can be difficult to determine what is actually going on under the hood, so to speak. The choice should be based on which algorithm (the above vs some other possibilities) is simplest. How would you do this by copying to another array? /Al |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Inserting or deleting elements in an array Al Dunbar schrieb: Quote: > > "JJ" <jim.mcatee@xxxxxx> wrote in message > news:c2146673-86cb-4da3-bd1c-dfa6b69da6ab@xxxxxx Quote: >> Is there a simple way to either insert elements into an array or to >> delete them, without copying the array to another? Efficiency is of >> no real concern in this application, as the arrays consist of under 50 >> elements and only one or two insertions or deletions are ever >> necessary. > You can certainly insert and delete elements in an array without > creating a new array. Sort of. Here's how: > > xxx is an array of 50 elements (0 through 49), and you want to delete > element 17: > > for i = 17 to 48 > xxx(i) = xxx(i+1) > next > redim xxx(48) > > To insert it back: > > for i = 49 to 18 step -1 > xxx(i) = xxx(i-1) > next > redim xxx(49) > > Obviously, you'd develop a sub or function to do this, and use the > actual length of the array instead of the literal 48 and 49. > > Of course, in reality, the redim statement *is* creating a new array. > > Regardless, you say that "efficiency" is not an issue. I agree that the > choice of algorithm should not be based on "efficiency", as it can be > difficult to determine what is actually going on under the hood, so to > speak. The choice should be based on which algorithm (the above vs some > other possibilities) is simplest. > > How would you do this by copying to another array? > > /Al to roll your own array manipulation subs, start with something like: Dim aTest : aTest = Array( 1, 2, 3 ) WScript.Echo "A", Join( aTest, " " ) deleteA aTest, 1, 1 WScript.Echo "D", Join( aTest, " " ) insertA aTest, 1, "zwei" WScript.Echo "I", Join( aTest, " " ) insertA aTest, 0, "zero" WScript.Echo "I", Join( aTest, " " ) insertA aTest, 4, "four" WScript.Echo "I", Join( aTest, " " ) deleteA aTest, 3, 2 WScript.Echo "D", Join( aTest, " " ) deleteA aTest, 0, 1 WScript.Echo "D", Join( aTest, " " ) deleteA aTest, 0, 2 WScript.Echo "D", Join( aTest, " " ) Sub deleteA( aX, nWhere, nCnt ) Dim nUB : nUB = UBound( aX ) Dim nIdx For nIdx = nWhere + nCnt To nUB aX( nIdx - nCnt ) = aX( nIdx ) Next ReDim Preserve aX( nUB - nCnt ) End Sub Sub insertA( aX, nWhere, vWhat ) Dim nUB : nUB = UBound( aX ) ReDim Preserve aX( nUB + 1 ) Dim nIdx For nIdx = nUB To nWhere Step -1 aX( nIdx + 1 ) = aX( nIdx ) Next aX( nWhere ) = vWhat End Sub output: === manipArray: manipulate array ====== A 1 2 3 D 1 3 I 1 zwei 3 I zero 1 zwei 3 I zero 1 zwei 3 four D zero 1 zwei D 1 zwei D === manipArray: 0 done (00:00:00) ===== At least you can identify the spots where further work (error handling) is necessary. If you don't want to program, look at the .NET ArrayList. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Inserting or deleting elements in an array "ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message news:4a6dc336$0$32663$9b4e6d93@xxxxxx-online.net... Quote: > Al Dunbar schrieb: Quote: >> >> "JJ" <jim.mcatee@xxxxxx> wrote in message >> news:c2146673-86cb-4da3-bd1c-dfa6b69da6ab@xxxxxx Quote: >>> Is there a simple way to either insert elements into an array or to >>> delete them, without copying the array to another? Efficiency is of >>> no real concern in this application, as the arrays consist of under 50 >>> elements and only one or two insertions or deletions are ever >>> necessary. >> You can certainly insert and delete elements in an array without creating >> a new array. Sort of. Here's how: >> >> xxx is an array of 50 elements (0 through 49), and you want to delete >> element 17: >> >> for i = 17 to 48 >> xxx(i) = xxx(i+1) >> next >> redim xxx(48) >> >> To insert it back: >> >> for i = 49 to 18 step -1 >> xxx(i) = xxx(i-1) >> next >> redim xxx(49) >> >> Obviously, you'd develop a sub or function to do this, and use the actual >> length of the array instead of the literal 48 and 49. >> >> Of course, in reality, the redim statement *is* creating a new array. >> >> Regardless, you say that "efficiency" is not an issue. I agree that the >> choice of algorithm should not be based on "efficiency", as it can be >> difficult to determine what is actually going on under the hood, so to >> speak. The choice should be based on which algorithm (the above vs some >> other possibilities) is simplest. >> >> How would you do this by copying to another array? >> >> /Al > Without "Preserve" those "ReDim"s will zap your array. /Al |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Inserting or deleting elements in an array gosh guys, this "insert item" is getting REALLY COMPLICATED. I hesitated to suggest this before, but maybe the O.P. ought to consider xml as the way to go. There is a handy object all preprogrammed for you (by microsoft no less): "MSXML2.XMLHTTP". If you don't already have it, it is a free download on msdn. Conceeded, the learning curve is steep, but the effort is worth it. The DOMDocument class has a "InsertBefore" method, allowing you to insert an item anywhere you want. cheers, jw ____________________________________________________________ You got questions? WE GOT ANSWERS!!! ..(but, no guarantee the answers will be applicable to the questions) |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Can't delete elements in folder Recent elements | Vista General | |||
| Converting array elements between types | PowerShell | |||
| number of elements in array | PowerShell | |||
| Support for Adobe photoshop elements 5 or premier elements 3 on Vista 64 bits | Vista General | |||
| re-ordering elements in an array? | PowerShell | |||