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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find which element in an array is blank or blank line

Status
Not open for further replies.

Biggorron

Technical User
Oct 5, 2010
3
CA
hi there, i have a text file and it has some blank lines i am trying to find out what postion that blank line was put into the array. so as an examp

apple
banna
grapes

watermelon
cherry
strawberry

as we can see here the blank line would be in the 4th position of the array. how do i find that out heres
 
intCounter = 1
For Each aElement In aArray
If aElement = "" Then
'its not neat to exit loops but it makes sense here?
Wscript.Echo "blank found on line " & CStr(intCounter)
Exit For
End If
intCounter = intCounter + 1
Next
'i guess you might want something like
If UBound(aArray) = intCounter Then 'might need to be + 1
Wscript.Echo "no blank line found, prob not strictly true could be the last line?"
Else
Wscript.Echo "blank line found on " & CStr(intCounter)
End If

'this might be a better approach, in my mind i like to avoid the use of booleans in this type of thing, seems to not be the most elegant

'blnFound = False
inCounter = 1
For Each aElement In aArray
If aElement = "" Then
'its not neat to exit loops but it makes sense here?
Wscript.Echo "blank found on line " & CStr(intCounter)
blnFound = True
Exit For
End If
intCounter = intCounter + 1
Next

If blnFound = True Then
Wscript.Echo "found on line " & CStr(intCounter)
Else
Wscript.Echo "didnt find blank " & CStr(intCounter)
End If

'you could have multiple blank lines?
'see, i knew the bln and the Exit For were a bad idea
Set dicBlankLines = CreateObject("Scripting.FileSystemObject")
inCounter = 1
For Each aElement In aArray
If aElement = "" Then
'its not neat to exit loops but it makes sense here?
Wscript.Echo "blank found on line " & CStr(intCounter)
dicBlankLines.Add CStr(intCounter), ""
End If
intCounter = intCounter + 1
Next
'
If dicBlankLines.Count = 0 Then
Wscript.Echo "no blank lines found"
Else
Wscript.Echo "blank lines found"
For Each aElement In dicBlankLines
Wscript.Echo "blank line found on " & aElement
Next
End If
 
Just a quick question: why do you need to know where the blank lines are? I ask because if the purpose is to remove them, for example, there may be a simpler approach ...
 
Strongm, i think the blank line serves are a delimiter in the file, delimiting between the ingredients of cocktails
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top