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

finding pointer in for each loop

Status
Not open for further replies.

IknowMe

Programmer
Aug 6, 2004
1,214
US
What I'd like to do but not sure how.

for each line in myStringArrayList
if instr(line, "Here I am") then

'console.writeline Line index number

'I need the index number of the current
'line in the ArrayList

'can't use the IndexOf function because
'lines are not unique

end if
next

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
What about using the old fashion way:

Dim Line As String

For Index As Integer = 0 To myStringArrayList.Count - 1

Line = myStringArrayList(Index)

'Following code here
'Index gives the pointer

Next
 
If you are trying to find the item that matches an expression from an array, you don't want to iterate through "every" item. This just wastes process time, especially if the first one matches the criteria and you still have 1,000 items to iterate through.

Use a Do Until loop which can break out of the loop once you find your item that matches your criteria.

Code:
Dim Idx as Integer = -1, rtnIdx as Integer = -1
Do Until Idx = (myStringArrayList.Length -1) Or rtnIdx > -1
   Idx +=1
   Dim tmpString As String = myStringArrayList(Idx)
   If tmpString.IndexOf("Here I am") > -1 Then
      rtnIdx = idx
   End If
Loop

If rtnIdx > -1 Then
  'You Found It
  'It's myStringArrayList(rtnIdx)
Else
  'You Didn't Find it. :(
End If
 
for 0 to count does achieve what I'm after but I am curious if it can be done in a for each.

The Do Until will not as I do need to loop through the entire list for consideration and some entries are identical. In the Do Until i'll always jump out at the first instance.

I do appreciate both of you for taking the time to respond with your thoughts :)

I am assuming there must be some pointer iterating through the for each loop and ultimatley that's the one I'm after.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
I think that you should add a countersince your objects are not unique if your for each was for a collection of objects then indexof would "always" (nearly anyway) work.

Code:
dim c as integer = 0
for each line in myStringArrayList
   if instr(line, "Here I am") then

       'console.writeline Line index number

       'I need the index number of the current
       'line in the ArrayList
       'c is now the index.

       'can't use the IndexOf function because
       'lines are not unique
        c+=1
   end if
next

Christiaan Baes
Belgium

My Blog
 
Chrissie, thanks for your effort. That's currenlty the way the code is functioning. I omited it in the OP in hopes of avoiding the same path of thought and finding a way around the c+=1.

I know it's negligable but it's turned into more of a puzzle at this point. I know the For Each is incrementing somewhere and am trying to do is reveal it's current value. I'm fairly new to the ArrayList class and thought I may have been overlooking a property somewhere.

Again thanks to all who've responded.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top