ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Array element (https://www.excelbanter.com/excel-programming/318453-array-element.html)

Andrea[_8_]

Array element
 
hello,
Someone could tell me which is the instruction to delete
an Array element ? It could seems a silly question, but I
didn't find it inside the VB manual.
I submit a simply example to explain myself better:

For primat = 1 To 50
if newarr(primat) = 15 then "delete element"
Next primat

here I have a "newarr" Array and verifying its elements, I
want to delete all 15 values.
Many thanks.
Andrea.


P Daulton

Array element
 
if newarr(primat) = 15 then "delete element"
becomes
if newarr(primat) = 15 then newarr(primat) = 0
Pascal


"Andrea" wrote in message
...
hello,
Someone could tell me which is the instruction to delete
an Array element ? It could seems a silly question, but I
didn't find it inside the VB manual.
I submit a simply example to explain myself better:

For primat = 1 To 50
if newarr(primat) = 15 then "delete element"
Next primat

here I have a "newarr" Array and verifying its elements, I
want to delete all 15 values.
Many thanks.
Andrea.




Tom Ogilvy

Array element
 
a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.

Sub tester1()
Dim newarr(1 To 50)
For i = 1 To 50
If Rnd() < 0.25 Then
newarr(i) = 15
Else
newarr(i) = i
End If
Next
Range("A1:A50").Value = Application.Transpose(newarr)
j = 1
For primat = 1 To 50
If newarr(primat) < 15 Then
newarr(j) = newarr(primat)
j = j + 1
End If
Next

For i = j To 50
newarr(i) = 0
Next
Range("B1:B50").Value = Application.Transpose(newarr)

End Sub

--
Regards,
Tom Ogilvy

"Andrea" wrote in message
...
hello,
Someone could tell me which is the instruction to delete
an Array element ? It could seems a silly question, but I
didn't find it inside the VB manual.
I submit a simply example to explain myself better:

For primat = 1 To 50
if newarr(primat) = 15 then "delete element"
Next primat

here I have a "newarr" Array and verifying its elements, I
want to delete all 15 values.
Many thanks.
Andrea.




Alan Beban[_2_]

Array element
 
Andrea wrote:
hello,
Someone could tell me which is the instruction to delete
an Array element ? It could seems a silly question, but I
didn't find it inside the VB manual.
I submit a simply example to explain myself better:

For primat = 1 To 50
if newarr(primat) = 15 then "delete element"
Next primat

here I have a "newarr" Array and verifying its elements, I
want to delete all 15 values.
Many thanks.
Andrea.

I don't know what you mean by "verifying its elements"; but in any
event, in your workbook VB Editor click on Tools|References and check
Microsoft Scripting Runtime. Then, assuming you have a 1-based,
N-element, 1-D array named "newarr", call the following function with

DeleteElems newarr, 15

to delete the elements equal to 15 (if any) and reduce the upper bound
from N to N-the number of occurrences of 15:

Function DeleteElems(InputArray, ElemValue) As Boolean
Dim x As Dictionary, Elem As Variant
Dim Num As Long, i As Long
Set x = New Dictionary
Num = 0
For Each Elem In InputArray
x.Add CStr(Num), Elem
If Elem = 15 Then x.Remove (CStr(Num))
Num = Num + 1
Next
InputArray = x.Items
ReDim Preserve InputArray(1 To UBound(InputArray) + 1)
End Function

Alan Beban

Alan Beban[_2_]

Array element
 
Tom Ogilvy wrote:
a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.


If you substitute the following for the second loop you probably have it:

ReDim Preserve newarr(1 To j - 1)

Alan Beban

Tom Ogilvy

Array element
 
Depends on whether i want to resize the array or not. Delete would imply
that but is not definitive. Resizing, as you know, but maybe the OP
doesn't, would require that the array be dynamic. Looping with hard coded
bounds would imply fixed - but again, specifics were not provided.

--
Regards,
Tom Ogilvy

"Alan Beban" wrote in message
...
Tom Ogilvy wrote:
a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.


If you substitute the following for the second loop you probably have it:

ReDim Preserve newarr(1 To j - 1)

Alan Beban





All times are GMT +1. The time now is 09:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com