|
-
Nov 29th, 2010, 03:49 PM
#1
Thread Starter
Hyperactive Member
For Each problem
Is there a way, other than using a counter variable, to tell what iteration you're on when in a For Each loop? In this case the For Each loop concatenates each value onto a comma-delimited string and right now it's incorrectly starting with a comma. I'd like to skip the concatenation on the 1st loop, like I'd do in a For/Next loop:
For x = 1 to 100
if x = 1 then
y = z
else
y = y & ", " & z
end if
Next x
-
Nov 29th, 2010, 04:36 PM
#2
Re: For Each problem
It's an interesting point, and one that I have spent a very short amount of time looking at...then switched to a For...Next loop.
There would be ways to do the job as long as each item in the loop is unique, which will be true for certain types of collections. If you are looping through a collection of reference types, you would be able to check if the current item IS the first item in the collection. For value types you would be able to check whether the current item equals the first item in the collection, but that will fail if the items are not unique. I know of no other way to do it.
My usual boring signature: Nothing
 
-
Nov 29th, 2010, 05:32 PM
#3
Thread Starter
Hyperactive Member
Re: For Each problem
Well, shoot. Thanks for your insight, Shaggy. It doesn't sound like I'll be able to directly check in this case. If this were a subscripted array I could initialize the accumulator with the 1st value, then use a For/Next loop starting with the 2nd. Or maybe I'll append the comma to each value then strip the final comma after the loop ends. That would probably be the most efficient even if it is a little bit weird.
Thanks.
-
Nov 29th, 2010, 07:19 PM
#4
Re: For Each problem
" Or maybe I'll append the comma to each value then strip the final comma after the loop ends. That would probably be the most efficient even if it is a little bit weird."
Whether it's efficient or not (and I don't think it's that big of a deal) it is the most common way to perform the action. The other way is to check the value of y ... if it's nothing then just assign it a value. if it isn't empty, then append a comma, then the next value.
-tg
-
Nov 30th, 2010, 04:17 AM
#5
Re: For Each problem
 Originally Posted by calvin-c
Well, shoot. Thanks for your insight, Shaggy. It doesn't sound like I'll be able to directly check in this case. If this were a subscripted array I could initialize the accumulator with the 1st value, then use a For/Next loop starting with the 2nd. Or maybe I'll append the comma to each value then strip the final comma after the loop ends. That would probably be the most efficient even if it is a little bit weird.
Thanks.

Code:
y = z(1) '~~~ Assign the first item to y
For x = 2 to 100
y = y & ", " & z(x)
Next x
'~~~ There won't be a comma at the end !
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 30th, 2010, 06:24 AM
#6
Frenzied Member
Re: For Each problem
both stripping the leading comma from the front and occassionally the rear are common fixes for such algorythms..
I would go with the latter do the odd/special things and walk through the rest so the last answer would be a fine example of that..
but if you are as the answers suggest going from an array to a comma separated string the best solution is a built in function pair called split and join
split strips a string into an array and join concatenates an array into a string
syntax varies but if available theyy are something like...
arrayname=split(string,separator)
string=join(arrayname,separator)
so for you
outputstring=join(inputarray,",")
all done no loops required
-
Nov 30th, 2010, 07:01 AM
#7
Re: For Each problem
For these sorts of things I like to use StringBuilder
Code:
Dim sb As New System.Text.StringBuilder
Dim tv() As Integer = New Integer() {1, 2, 3, 4, 5}
For Each foo As Integer In tv
sb.Append(foo.ToString) 'not foo.ToString & ","
sb.Append(",")
Next
sb.Length = sb.Length - 1
Dim s As String = sb.ToString
If the items are strings then Join is appropriate as pointed out by incidentals.
Code:
Dim s() As String = New String() {"1", "2", "3"}
Dim outS As String = String.Join(",", s)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|