Deleting an element in the middle of an array

M

Mike NG

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
 
M

Mike NG

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
 
B

Bob Phillips

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)
 
R

RB Smissaert

Yes, didn't think there was.
Luckily, just looping through the array elements is quite fast.

RBS

Bob Phillips said:
We wish :)

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
M

Mike NG

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
 
D

Dana DeLouis

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
 
B

Bob Phillips

Aah yes. Then it is shunt and redim for you I think.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
M

Mike NG

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
 
T

Tom Ogilvy

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.
 
M

Mike NG

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
 
T

Tom Ogilvy

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)
 
A

Alan Beban

Mike said:
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
 
M

Mike NG

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top