473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select distinct fields in an array

375 256MB
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)="Tata"
Vehicle(1)="Tata"
Vehicle(2)="Tata"
Vehicle(3)="Maruti"
Vehicle(4)="Maruti"


and so on

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

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
Jul 26 '07 #1
8 13417
Killer42
8,435 Expert 8TB
I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
Expand|Select|Wrap|Line Numbers
  1. Dim Array2Count As Long, Used As Boolean
  2. Dim I As Long, J As Long
  3. For I = 1 To 100
  4.   Used = False
  5.   For J = 1 To Array2Count
  6.     If Array2(J) = Array1(I) Then
  7.       Used = True
  8.       Exit For
  9.     End If
  10.   Next
  11.   If Not Used Then
  12.     Array2Count = Array2Count + 1
  13.     Array2(Array2Count) = Array1(I)
  14.   End If
  15. Next
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.
Jul 26 '07 #2
cmrhema
375 256MB
I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
Expand|Select|Wrap|Line Numbers
  1. Dim Array2Count As Long, Used As Boolean
  2. Dim I As Long, J As Long
  3. For I = 1 To 100
  4.   Used = False
  5.   For J = 1 To Array2Count
  6.     If Array2(J) = Array1(I) Then
  7.       Used = True
  8.       Exit For
  9.     End If
  10.   Next
  11.   If Not Used Then
  12.     Array2Count = Array2Count + 1
  13.     Array2(Array2Count) = Array1(I)
  14.   End If
  15. Next
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=vehicleid+","+actualvehicleid

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
Jul 28 '07 #3
Killer42
8,435 Expert 8TB
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?
Jul 29 '07 #4
cmrhema
375 256MB
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.
Jul 29 '07 #5
Killer42
8,435 Expert 8TB
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.
Jul 30 '07 #6
cmrhema
375 256MB
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,marut etc.
Now I will the below function
and can manipulate whenever and whatever way i need it

Expand|Select|Wrap|Line Numbers
  1. string MainString = finalvehicleid;
  2.            string[] Split = MainString.Split(new Char[] { ' ,' });
  3.            int s1 = Split.Length;
  4.            string final;
  5.  
  6.             for (int i = 0; i < s1; i++)
  7.  
  8.             {
  9.                 final = Split[i].ToString();
  10.                 if (final != ",")
  11. // in case there are words which may (rarely) follow two quotes then i will not include it.
  12. //i will store these vehicle id and send mail to the required person using smtp
  13. //At present i have used message box but i will be required to display all the vehicles in a textbox
  14. //Because the vehicle id were not stored in a database i could use the select distinct query
  15.  
  16.                 {
  17.                     MessageBox.Show(final);
  18.                 }
  19.  
  20.  
  21.             }
Jul 30 '07 #7
Killer42
8,435 Expert 8TB
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.
Jul 30 '07 #8
cmrhema
375 256MB
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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  RowCount = (Ds.Tables("ChkTable").Rows.Count)
  3.                 UserId = Ds.Tables("chktable").Rows(0)(2)
  4.                 RegNo = Ds.Tables("chktable").Rows(0)(0)
  5.                 TempUserId = UserId
  6.                 TempStoreRegno = ""
  7. RowCount = (Ds.Tables("ChkTable").Rows.Count)
  8.                 For I = 0 To RowCount - 1
  9.                     RegNo = (Ds.Tables("chktable").Rows(I)(0))
  10.                     dataTime = (Ds.Tables("chktable").Rows(I)(1))
  11.                     UserId = (Ds.Tables("chktable").Rows(I)(2))
  12. 'Some calculations take place
  13.  'Send email to the User stating that his vehicle bearing this registration No is idle
  14.                     If (TempUserId = Ds.Tables("chktable").Rows(I)(2)) Then
  15.                         TempStoreRegno = TempStoreRegno + " " + Ds.Tables("chktable").Rows(I)(0)
  16.                     Else
  17.                         Call Display()
  18. 'call a function
  19.                         TempUserId = Ds.Tables("chktable").Rows(I)(2)
  20.                         TempStoreRegno = Ds.Tables("chktable").Rows(I)(0)
  21.                     End If
  22.                 Next
  23.                 Call Display()
  24. Call MessageMail
  25. '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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Display()
  2.         TempUserId1 = (Ds.Tables("chktable").Rows(I - 1)(2))
  3.         SuperUserId = SuperUserId + " " + vbCrLf + TempUserId1 + "  " + TempStoreRegno
  4.         Registrationno = Registrationno + vbCrLf + TempStoreRegno
  5.     End Sub
Hope I have cleared your doubts.
Thanks
Jul 30 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: shumaker | last post by:
I was curious... Is there a way to select distinct on a combination of some fields and the for each record returned also get the other fields of an arbitrarily chosen record matching the fields...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
8
by: skinnybloke | last post by:
Hi - I have a problem with a memo field being truncated to about 255 characters when running a Access 2002 query. This only seems to happen if I use SELECT DISTINCT. It works ok using SELECT by...
3
by: orekinbck | last post by:
Hi There Our test database has duplicate data: COMPANYID COMPANYNAME 1 Grupple Group 2 Grupple Group 5 Grupple Group 3 Yada Inc 4 Yada...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
6
by: pooh80133 | last post by:
Hi! I am pasting my SQL code at the end of this message. I am trying to use SELECT DISTINCT in a query, but I am a beginner for using Access. Right now I have duplicate ID's (Indiv ID) in my...
5
by: justanothernewbie804 | last post by:
hi all. I have a dataset and would like to do something like "select distinct field1, field2 from fields" against the dataset. I do not have the option of creating a new database table and...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
8
by: penfold33 | last post by:
Hi I need to generate a CSV of names and address from a table and I would like only one result (it doesn't matter which) per distinct email address. If possible, they also need to be ordered by...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.