Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set array element to empty Raul Excel Programming 2 March 2nd 05 03:53 PM
Array Element question S G Booth Excel Programming 5 February 23rd 05 09:59 PM
Array element Andrea[_8_] Excel Programming 5 December 7th 04 08:24 PM
Deleting data and element in a 1D array ExcelMonkey[_124_] Excel Programming 11 May 17th 04 06:29 AM
deleting array element michael Excel Programming 0 December 18th 03 08:55 PM


All times are GMT +1. The time now is 11:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"