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

ArrayList string comparison

Status
Not open for further replies.

bud31047

Technical User
Dec 3, 2007
44
0
0
US
I am parsing a text file and storing the results in an ArrayList. Prior to appending the data I need to compare the string in the current and next index looking for duplicate/unwanted data.

Here is what the data in the ArrayList looks like;

Alice Smith;34578;01/01/2010;10:10:00 PM;;Add Punch
Alice Smith;34578;01/01/2010;10:10:00 PM;;Delete Punch
Brian Smith;35678;01/02/2010;7:30:00 AM;;Add Punch
Brian Williams;37978;01/02/2010;7:30:00 AM;;Edit Punch
Brian Williams;37978;01/02/2010;7:30:00 AM;;Delete Punch

I need to add the following logic to my code prior to appending the data from the array.

For Each i as String in MyArrayList
If Not lines(i).Contains("Add Punch") & _
Not lines(i+1).Contains("Delete Punch") then
If Not lines(i).Substring(everything left of the 5th ";") =
lines(i+1).Substring(everything left of the 5th ";") then
System.IO.File.AppendAllText("D:\ParsedFile.txt", s & Environment.NewLine)
i=i+1 'skip to the third index
next

Simply put I need to ignore the two strings for Alice because a time card punch was added and then deleted for the same person and for the same time period. Can this be done while the data is still in the arraylist?

Thanks in advance for the help,
Bud
 
Consider a DataTable where you can execute SELECT, UPDATE, INSERT, DELETE statements against.

--------------------------------------------------
Stubbornness is a virtue -- if you are right. --Chuck Noll
--------------------------------------------------
 
You can't modify the object that you're enumerating through, in this case the ArrayList. You can use another ArrayList and write to that...

Or...

Why not provide the logic while you are building the ArrayList? That is read each line and see if it's already in the ArrayList... if so, ignore it, so you only add the lines once.

By using two ArrayLists you are using up to twice the memory (assuming no mispunches) and having to do double the work... reading the data in once, and then filtering it; while you could just filter it while you read it.

Also, instead of doing the .substring(everything to the left of the 5th ';'), use split() and split on the ';'. Then you just have to use the 6th object in that array for that column.

Is what you're trying to accomplish just to clean up a text file so that extra punches/edits are not shown?
 
NEWSalt,
I had completed the program but someone came back after the fact and wanted me to filter the output.

I will explore the idea of using another ArrayList. At this point I think this would be a better solution than re-writing the program. Memory is not an issue as the source file is less than 500k.

Thanks for the information guys.

Bud
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top