Editing an array

B

Bear

I load a file into a string array using the Split funcsion in this statement:

arrDraft() = Split(Expression:=docDraft.Range.Text, Delimiter:="<")

Most of the "<" characters were inserted to number the sentences but some
may be spurious Word shrapnel or may have been entered by the author of
docDraft.

So say there were five "good" hits and then a "junk" hit. The arrDraft might
look like:

1>Here is the first sentence.
2>Second sentence.
3>Third sentence.
4>Fourth sentence.
5>Fifth sentence.
The author enclosed this in angle brackets.>
6>The sentence after the author's brackets.
etc.

I'd like to "clean up" the array before using it. It there any other or
better way than by looping through each element, testing it, and if it's good
passing it into another array? Or can elements from this array be deleted
dynamically?

Bear
 
J

Jay Freedman

Hi Bear,

If you're using a Variant as an array this way, there is no means of
deleting items from it. What you can do, though, is copy valid items
from higher numbers to overwrite the "junk", leaving only empty
strings at the top end of the array, like this:

Sub x()
Dim arrDraft As Variant
Dim i As Long, j As Long

arrDraft = Split(Expression:=docDraft.Range.Text, _
Delimiter:="<")

For i = UBound(arrDraft) To 1 Step -1
If Not IsNumeric(Left$(arrDraft(i), 1)) Then
For j = i To UBound(arrDraft) - 1
arrDraft(j) = arrDraft(j + 1)
Next j
arrDraft(UBound(arrDraft)) = ""
End If
Next i

' just to demonstrate the result
docDraft.Range.InsertAfter vbCr & vbCr & Join(arrDraft, vbCr)
End Sub

You can then write your subsequent processing to stop when it finds
the first empty string in the array.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

Bear,

I don't see any way of testing each entry without cycling through each one
using a loop:

Sub ScratchMaco()
Dim arrTest() As String
Dim i As Long
Dim pStr As String
arrTest() = Split("Test sentence.>Test sentence.>Tag>Test sentence.>Test
sentence.", ">")
For i = LBound(arrTest) To UBound(arrTest)
If i > UBound(arrTest) Then Exit For
pStr = arrTest(i)
'Your test condition would go here. For example, I am testing a period at
the end of the string.
If Not Right(pStr, 1) = "." Then DeleteElement arrTest, i
Next i
For i = LBound(arrTest) To UBound(arrTest)
MsgBox arrTest(i)
Next i
End Sub

Sub DeleteElement(arrInput() As String, Element As Long)
Dim Counter As Long
For Counter = Element To UBound(arrInput) - 1
arrInput(Counter) = arrInput(Counter + 1)
Next Counter
'Resize the array
ReDim Preserve arrInput(LBound(arrInput) To UBound(arrInput) - 1)
'Down increment counter
Element = Element - 1
End Sub
 
T

Tony Jollans

What are your criteria for identifying 'junk' records? If you have code to
identify them after the split, it should be possible to use similar code to
identify (and remove) the strings before the split - it may be more trouble
than it's worth, though.
 
B

Bear

Jay, Greg, and Tony:

Thanks for your suggestions -- all worthwhile ideas! Once again, there's no
"easy" way to do what I want -- just the "doable" way. So I think I'll stay
with my original processing loop, but test for invalid array items and skip
them when I find them.

I'll skip them by putting an address just before the Next statement and
going to the address if any test for junk fails. If the item passes all
tests, then I'll process it.

Thanks again,

Bear
 

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