Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
I have my own dynamic array
Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array -- Mike |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 05:14:31, Mike NG (Mike NG
) wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array I've also tried Links.Remove 5, but this doesn't appear to work It appears as though I have to filter my array to a new array and copy everything over manually -- Mike |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Mike,
There is a simpler way using Filter. Here is an example Dim vMatch Dim i As Long Dim ary ary = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i vMatch = Chr(255) ary(7) = vMatch ' change the 8 ary = Filter(ary, vMatch, False) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i -- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 05:14:31, Mike NG (Mike NG ) wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array I've also tried Links.Remove 5, but this doesn't appear to work It appears as though I have to filter my array to a new array and copy everything over manually -- Mike |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Is there a Filter function that works with 2-D arrays?
RBS "Bob Phillips" wrote in message ... Mike, There is a simpler way using Filter. Here is an example Dim vMatch Dim i As Long Dim ary ary = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i vMatch = Chr(255) ary(7) = vMatch ' change the 8 ary = Filter(ary, vMatch, False) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i -- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 05:14:31, Mike NG (Mike NG ) wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array I've also tried Links.Remove 5, but this doesn't appear to work It appears as though I have to filter my array to a new array and copy everything over manually -- Mike |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
We wish :-)
-- HTH RP (remove nothere from the email address if mailing direct) "RB Smissaert" wrote in message ... Is there a Filter function that works with 2-D arrays? RBS "Bob Phillips" wrote in message ... Mike, There is a simpler way using Filter. Here is an example Dim vMatch Dim i As Long Dim ary ary = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i vMatch = Chr(255) ary(7) = vMatch ' change the 8 ary = Filter(ary, vMatch, False) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i -- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 05:14:31, Mike NG (Mike NG ) wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array I've also tried Links.Remove 5, but this doesn't appear to work It appears as though I have to filter my array to a new array and copy everything over manually -- Mike |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Yes, didn't think there was.
Luckily, just looping through the array elements is quite fast. RBS "Bob Phillips" wrote in message ... We wish :-) -- HTH RP (remove nothere from the email address if mailing direct) "RB Smissaert" wrote in message ... Is there a Filter function that works with 2-D arrays? RBS "Bob Phillips" wrote in message ... Mike, There is a simpler way using Filter. Here is an example Dim vMatch Dim i As Long Dim ary ary = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i vMatch = Chr(255) ary(7) = vMatch ' change the 8 ary = Filter(ary, vMatch, False) For i = LBound(ary) To UBound(ary) Debug.Print ary(i) Next i -- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 05:14:31, Mike NG (Mike NG ) wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array I've also tried Links.Remove 5, but this doesn't appear to work It appears as though I have to filter my array to a new array and copy everything over manually -- Mike |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 12:02:32, Bob Phillips (Bob Phillips
) wrote: We wish :-) Cheers Bob Nice idea - except my array of type LinkInfo is effectively 2D isn't it? -- Mike |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Why?
-- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 12:02:32, Bob Phillips (Bob Phillips ) wrote: We wish :-) Cheers Bob Nice idea - except my array of type LinkInfo is effectively 2D isn't it? -- Mike |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 12:42:22, Bob Phillips (Bob Phillips
) wrote: Why? Because I need to filter out elements where the CatCode element is a certain value - Filter only works on flat 1D arrays, doesn't it? Type LinkInfo CatCode as Integer href As String InnerText As String End Type -- Mike |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Filter only works on flat 1D arrays, doesn't it?
Yes it does. In addition: v = Array(1, 2, 3, 4, 5) v = Filter(v, 3, False) With this example, the remaining integers in v are converted to strings For certain complicated problems, I like to use a Dictionary object. However, would a "Collection" work for you? Not the best solution, so I'll just throw this out to give some ideas. This quick and dirty example adds 5 records with 3 fields. Sub Demo() Dim c As New Collection Dim P As Long '// Add Records '// Records Fields: CatCode ,href, InnerText c.Add Array("w", 1, "one"), CStr(1) c.Add Array("x", 3, "two"), CStr(2) c.Add Array("y", 5, "three"), CStr(3) c.Add Array("z", 7, "four"), CStr(4) c.Add Array("x", 9, "five"), CStr(5) '// Delete CatCode's with "x" For P = c.Count To 1 Step -1 If c(P)(0) = "x" Then c.Remove (P) Next P '// Two records removed: For P = 1 To c.Count Debug.Print c(P)(0), c(P)(1), c(P)(2) Next P End Sub HTH -- Dana DeLouis Win XP & Office 2003 "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 12:42:22, Bob Phillips (Bob Phillips ) wrote: Why? Because I need to filter out elements where the CatCode element is a certain value - Filter only works on flat 1D arrays, doesn't it? Type LinkInfo CatCode as Integer href As String InnerText As String End Type -- Mike |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Aah yes. Then it is shunt and redim for you I think.
-- HTH RP (remove nothere from the email address if mailing direct) "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 12:42:22, Bob Phillips (Bob Phillips ) wrote: Why? Because I need to filter out elements where the CatCode element is a certain value - Filter only works on flat 1D arrays, doesn't it? Type LinkInfo CatCode as Integer href As String InnerText As String End Type -- Mike |
#12
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 09:24:23, Dana DeLouis (Dana DeLouis
) wrote: Filter only works on flat 1D arrays, doesn't it? Yes it does. In addition: Thanks I will try this later Since I call a routine to add stuff to my array already, I can't see this being a big change I already do some processing on the UBound of the array, so if I just change this to a collection it looks straight forward. My main area of concern was doing this all in the right order - usually starting with the top of the array like you have, and then working down, but wasn't too sure if it was going to work I will report back later -- Mike |
#13
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
since you use the term href this would indicate that href is a unique
identifier. If so, you can ignore order and capitalize on the unique nature of href. I actually thought this is where Dana was going with this which is the real advantage of using a collection. Sub Demo() Dim c As New Collection Dim P As Long '// Add Records '// Records Fields: href, InnerText c.Add Array("z", "one"), "z" c.Add Array("w", "two"), "w" c.Add Array("x", "three"), "x" c.Add Array("y", "four"), "y" c.Add Array("v", "five"), "v" '// Delete href "x" c.Remove "x" '// one records removed: For P = 1 To c.Count Debug.Print c(P)(0), c(P)(1) Next P End Sub If you wanted to eliminate it by position c.Remove 3 works as well. -- Regards, Tom Ogilvy "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 09:24:23, Dana DeLouis (Dana DeLouis ) wrote: Filter only works on flat 1D arrays, doesn't it? Yes it does. In addition: Thanks I will try this later Since I call a routine to add stuff to my array already, I can't see this being a big change I already do some processing on the UBound of the array, so if I just change this to a collection it looks straight forward. My main area of concern was doing this all in the right order - usually starting with the top of the array like you have, and then working down, but wasn't too sure if it was going to work I will report back later -- Mike |
#14
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 11:29:06, Tom Ogilvy (Tom Ogilvy
) wrote: For P = 1 To c.Count Debug.Print c(P)(0), c(P)(1) Next P is this the best way of doing it using for p = 1 to count? I thought most arrays in excel were 0 based, and it was safer to use for p in each c even the Filter method used will create the resulting array as 0 based (I presume even if you have option base 1 set) the above would be very useful, as I need to do some basic sorting, you know the type where you go for i = 1 to c.count - 1 for j = i+1 to c.count if c(i) c(j) then swap c(i) and c(j) round endif next next -- Mike |
#15
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
John Walkenbach uses a bublesort to sort a collection at:
http://j-walk.com/ss/excel/tips/tip47.htm Mike NG wrote: On Sun, 13 Mar 2005 at 11:29:06, Tom Ogilvy (Tom Ogilvy ) wrote: For P = 1 To c.Count Debug.Print c(P)(0), c(P)(1) Next P is this the best way of doing it using for p = 1 to count? I thought most arrays in excel were 0 based, and it was safer to use for p in each c even the Filter method used will create the resulting array as 0 based (I presume even if you have option base 1 set) the above would be very useful, as I need to do some basic sorting, you know the type where you go for i = 1 to c.count - 1 for j = i+1 to c.count if c(i) c(j) then swap c(i) and c(j) round endif next next -- Mike -- Dave Peterson |
#16
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
c isn't an array, it is a collection. p, an index number, is used to
identify an element of the collection C. The element contains an array which is zero based; evident c(P)(0) -- Regards, Tom Ogilvy "Mike NG" wrote in message ... On Sun, 13 Mar 2005 at 11:29:06, Tom Ogilvy (Tom Ogilvy ) wrote: For P = 1 To c.Count Debug.Print c(P)(0), c(P)(1) Next P is this the best way of doing it using for p = 1 to count? I thought most arrays in excel were 0 based, and it was safer to use for p in each c even the Filter method used will create the resulting array as 0 based (I presume even if you have option base 1 set) the above would be very useful, as I need to do some basic sorting, you know the type where you go for i = 1 to c.count - 1 for j = i+1 to c.count if c(i) c(j) then swap c(i) and c(j) round endif next next -- Mike |
#17
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Mike NG wrote:
I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array Is Links a one-column array? Alan Beban |
#18
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Sun, 13 Mar 2005 at 12:41:22, Alan Beban (Alan Beban
) wrote: Mike NG wrote: I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array Is Links a one-column array? Depends what you mean, but each element of the Links() array is of type LinkInfo I have decided to carry on using arrays. For the sake of being able to use add and delete easily, referencing the elements as (0), (1), (2) etc is not as friendly as using .href, .Innertext etc -- Mike |
#19
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
If you have a need to remove an element from the middle of an 'array'
then an array is probably the wrong data type structure to begin with. Consider a linked list. How To Implement a Linked List in Visual Basic http://support.microsoft.com/default...b;en-us;166394 -- Regards, Tushar Mehta www.tushar-mehta.com Excel, PowerPoint, and VBA add-ins, tutorials Custom MS Office productivity solutions In article , says... I have my own dynamic array Type LinkInfo href As String InnerText As String End Type Dim Links() As LinkInfo which I am redimensioning as I go along but I have the need to delete an array element, e.g. links(5) from the middle of the array when its upper bound is 10 How do I do this please. I can only find the erase method to purge the whole array |
#20
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
On Mon, 14 Mar 2005 at 19:25:10, Tushar Mehta (Tushar Mehta
) wrote: If you have a need to remove an element from the middle of an 'array' then an array is probably the wrong data type structure to begin with. Consider a linked list. How To Implement a Linked List in Visual Basic http://support.microsoft.com/default...b;en-us;166394 Cool never thought of that May consider this for a future project -- Mike |
#21
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting an element in the middle of an array
Hi,
you might want to investigate the Scripting.Dictionary object as well. Hth, O |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Set array element to empty | Excel Programming | |||
Array Element question | Excel Programming | |||
Array element | Excel Programming | |||
Deleting data and element in a 1D array | Excel Programming | |||
deleting array element | Excel Programming |