Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete An Array Element

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

Does anyone out there know a way to delete an array element from an array?

Background: I split a string into an array so I could get a portion of it into a variable. I don't need that portion of the array anymore so I blanked it out. However, now I just want to get rid of the blank entry in my array. Following is the code:


dim strRequest 'desired string
dim strFound 'boolean
dim numberChar 'variable holder
dim theParts 'Array
dim theString

theString = "10.11.130.80 - - [01/May/2001:13:52:59 -0600] 'GET /insuredacct/index.cfm HTTP/1.0' 200 355 "

'split string into Array Parts, array automatically creates
theParts = split(theString,"'")
strFound = False
For i = LBound(theParts) to UBound(theParts)
numberChar = left(theParts(i), 4)
response.write &quot;<br>&quot; & numberChar
If strcomp(numberchar, &quot;GET &quot;)=0 then
'assign that part to the strRequest variable
strRequest = theParts(i)
strFound = True
'now that we located strRequest, replace its section in array with blanks
theParts(i) = &quot; &quot;
response.write &quot;<br>&quot; & &quot;String was replaced with blanks.&quot;
exit for 'after finding string, exit loop
End If
Next

'**********************
'REVIEW PARTS POST-SEARCH
'**********************
response.write &quot;<hr></hr>&quot;
response.write &quot;<B>Part Review (After Search)</B> <br>&quot;
For i = LBound(theParts) to UBound(theParts)
response.write theParts(i) & &quot;<br>&quot;
Next
response.write &quot;<hr></hr>&quot;



If I print out my Array, here's what I have:


Part Review (After Search)
10.11.130.80 - - [01/May/2001:13:52:59 -0600]

200 355



All I want now is:


Part Review (After Search)
10.11.130.80 - - [01/May/2001:13:52:59 -0600]
200 355


Because I'll have to split these up too based on spaces.

Your help is greatly appreciated.

Thanks in advance,
scripter73

 
The only way to &quot;delete&quot; an element is to copy the array except for that element. How about &quot;marking&quot; it with data that the user could not enter
Code:
theParts(i) = Chr(8) ' Clear entry to BACKSPACE 
..........
For i = LBound(theParts) to UBound(theParts)
    if theParts(I) <> Chr(8) then
        response.write theParts(i) & &quot;<br>&quot;    
    End If
Next
 
Thanks, John!

The copying of the current array to another array sounds like a good idea. I've been playing around with it.

Here's a snippet of my code:

..theParts array already exists
dim item
dim theParts2

for each item in theParts
if item <> &quot; &quot; then
theParts2 = item
response.write item
end if
next


However, I think I'm having some trouble with my syntax. ASP complains about the type of theParts2 variable. Do I have to specifically assign it a dimension? I wanted it to be based on the length of the array I'm trying to copy (i.e., dynamic).

Here's what I'm trying to do. Read the elements of my existing array, and copy only those elements that aren't blank. Currently, it's duplicating some of my data.

Thanks for your help,
scripter73

 
WARNING: After SPLIT you must check IF NOT ISARRAY(theparts) or IF theparts = EMPTY (or is that IS EMPTY)
to detect &quot;&quot; input.
If you want to keep theparts and theparts2 then
Code:
Dim I
Dim J
Dim theparts2
J = -1 
Redim theParts2(ubound(theParts))
for I = 0 to theparts
    if theparts(I) <> &quot; &quot; then
        J = J + 1
        theparts2(J) = theparts(I)
    end if
Next 
Redim Preserve theparts2(J) ' Resize down
if you do not need theparts after deleteing then
Code:
Dim I
Dim J
J = -1 
for I = 0 to theparts
    if theparts(I) <> &quot; &quot; then ' Copy back over deleted
        J = J + 1
        if J <> I theparts(J) = theparts(I) &quot; No copy to self
    end if
Next 
Redim Preserve theparts(J) 'Resize down
I do not understand why you can not just bypass &quot;deleted&quot; theparts entries when doing the RESPONSE.WRITE..but hey, I don't know everything...yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top