Select distinct fields in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Select distinct fields in an array

    Hello
    I am preparing an application where I have different data stored in an array
    eg. Dim Vehicle(100) as String
    In this array different values are stored in an ordered manner.

    eg. Vehicle(0)="Tat a"
    Vehicle(1)="Tat a"
    Vehicle(2)="Tat a"
    Vehicle(3)="Mar uti"
    Vehicle(4)="Mar uti"


    and so on

    I want only distinct values of the array to be stored in another array
    eg. Vehicle(100)
    Vehicle(0)="Tat a"
    vehicle(1)="mau rti"

    How can I select distinct fields from an array?
    These are not stored in database, hence I cannot used the "select distinct" query.

    Kindly help
    Thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
    [CODE=vb]Dim Array2Count As Long, Used As Boolean
    Dim I As Long, J As Long
    For I = 1 To 100
    Used = False
    For J = 1 To Array2Count
    If Array2(J) = Array1(I) Then
    Used = True
    Exit For
    End If
    Next
    If Not Used Then
    Array2Count = Array2Count + 1
    Array2(Array2Co unt) = Array1(I)
    End If
    Next[/CODE]Of course I haven't tested this, just typed it here as I made it up. You may need to play with it a bit. Note, while VB prefers to start arrays at zero, I always start my arrays at one. You may need to adjust the code to suit your way of working.



    Another option would be to add each entry to a collection, using the vehicle as the key. Duplicates will be rejected, and you can use On Error processing to skip over them. This is probably more bother than it's worth, though.

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Originally posted by Killer42
      I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
      [CODE=vb]Dim Array2Count As Long, Used As Boolean
      Dim I As Long, J As Long
      For I = 1 To 100
      Used = False
      For J = 1 To Array2Count
      If Array2(J) = Array1(I) Then
      Used = True
      Exit For
      End If
      Next
      If Not Used Then
      Array2Count = Array2Count + 1
      Array2(Array2Co unt) = Array1(I)
      End If
      Next[/CODE]Of course I haven't tested this, just typed it here as I made it up. You may need to play with it a bit. Note, while VB prefers to start arrays at zero, I always start my arrays at one. You may need to adjust the code to suit your way of working.



      Another option would be to add each entry to a collection, using the vehicle as the key. Duplicates will be rejected, and you can use On Error processing to skip over them. This is probably more bother than it's worth, though.

      Thanks Killer

      Actually what I did was I created a temporary variable and checked all the elements in the loop .(Beacuse the elements in the loop were in ordered manner my problem got solved much quicker)
      If the element existed then move to next item in the loop
      else add the element in the variable
      eg. vehicleid=vehic leid+","+actual vehicleid

      So all the unique vehicle names were added to the variable
      and to separte all the vehicles I used the delimiter "," to seperate it

      Thanks indeed for your idea too

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        That's a clever idea. Wish I'd thought of it first. :)

        Of course, as you point out, the fact that it's a single variable does allow you to check it quicker and more simply. I expect you just used Instr() function for the check?
        Last edited by Killer42; Jul 30 '07, 02:00 AM. Reason: Typo

        Comment

        • cmrhema
          Contributor
          • Jan 2007
          • 375

          #5
          Originally posted by Killer42
          That's a clever idea. Wish I'd thought of it first. :)

          Of course, as you point out, he fact that it's a single variable does allow you to check it quicker and more simply. I expect you just used Instr() function for the check?
          Killer 42, Actually I was supposed to write the program in C#(it was changed by my project leader).
          And in C# you have a function called as split.
          I used that.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by cmrhema
            Killer 42, Actually I was supposed to write the program in C# (it was changed by my project leader).
            And in C# you have a function called as split.
            I used that.
            VB also has a Split() function, but I don't see the relevance.

            Comment

            • cmrhema
              Contributor
              • Jan 2007
              • 375

              #7
              Originally posted by Killer42
              VB also has a Split() function, but I don't see the relevance.
              the whole project was to be rewritten in C# console application
              all the vehicle id's after sorting and checking from the loop are stored in a variable called as vehicleid(which i have mentioned earlier)
              Now this will be stored in the manner tata,hyundai,ma rut etc.
              Now I will the below function
              and can manipulate whenever and whatever way i need it

              Code:
              string MainString = finalvehicleid;
                         string[] Split = MainString.Split(new Char[] { ' ,' });
                         int s1 = Split.Length;
                         string final;
                         
                          for (int i = 0; i < s1; i++)
                         
                          {
                              final = Split[i].ToString();
                              if (final != ",")
              // in case there are words which may (rarely) follow two quotes then i will not include it.
              //i will store these vehicle id and send mail to the required person using smtp
              //At present i have used message box but i will be required to display all the vehicles in a textbox
              //Because the vehicle id were not stored in a database i could use the select distinct query
                                          
                              {
                                  MessageBox.Show(final);
                              }
                              
              
                          }

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Correct me if I'm wrong, but doesn't that just split the string into an array, at the commas? You can certainly do the same thing with the Split() function in VB, but how is that any better than using an array in the first place, or adding to both at the same time?

                I was enquiring as to how you prevented adding duplicates, and I can't see how that routine would do so. Keeping in mind that I don't know C#, of course.

                Comment

                • cmrhema
                  Contributor
                  • Jan 2007
                  • 375

                  #9
                  Originally posted by Killer42
                  Correct me if I'm wrong, but doesn't that just split the string into an array, at the commas? You can certainly do the same thing with the Split() function in VB, but how is that any better than using an array in the first place, or adding to both at the same time?

                  I was enquiring as to how you prevented adding duplicates, and I can't see how that routine would do so. Keeping in mind that I don't know C#, of course.

                  because it is a live project i will not be able to reveal much, also i will produce the code in vb.net but without using split.

                  Now we have a program where i have sorted some datas and grouped it by registration no and vehicle no
                  Code:
                    
                   RowCount = (Ds.Tables("ChkTable").Rows.Count)
                                  UserId = Ds.Tables("chktable").Rows(0)(2)
                                  RegNo = Ds.Tables("chktable").Rows(0)(0)
                                  TempUserId = UserId
                                  TempStoreRegno = ""
                  RowCount = (Ds.Tables("ChkTable").Rows.Count)
                                  For I = 0 To RowCount - 1
                                      RegNo = (Ds.Tables("chktable").Rows(I)(0))
                                      dataTime = (Ds.Tables("chktable").Rows(I)(1))
                                      UserId = (Ds.Tables("chktable").Rows(I)(2))
                  'Some calculations take place
                   'Send email to the User stating that his vehicle bearing this registration No is idle
                                      If (TempUserId = Ds.Tables("chktable").Rows(I)(2)) Then
                                          TempStoreRegno = TempStoreRegno + " " + Ds.Tables("chktable").Rows(I)(0)
                                      Else
                                          Call Display()
                  'call a function
                                          TempUserId = Ds.Tables("chktable").Rows(I)(2)
                                          TempStoreRegno = Ds.Tables("chktable").Rows(I)(0)
                                      End If
                                  Next
                                  Call Display()
                  Call MessageMail
                  'This function will be used for some purpose
                  ' if here the function is NOT called the last group will not be recieved for further process. because when it encounter the NEXT statement in loop and there is none to be found it will exit the loop hence to record the last group we call the function after next for the last group

                  Code:
                  Private Sub Display()
                          TempUserId1 = (Ds.Tables("chktable").Rows(I - 1)(2))
                          SuperUserId = SuperUserId + " " + vbCrLf + TempUserId1 + "  " + TempStoreRegno
                          Registrationno = Registrationno + vbCrLf + TempStoreRegno
                      End Sub
                  Hope I have cleared your doubts.
                  Thanks

                  Comment

                  Working...