Results 1 to 7 of 7

Thread: For Each problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: For Each problem

    Quote Originally Posted by calvin-c View Post
    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,...

  6. #6
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    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

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width