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

Check stringarray (list) for existing value

Status
Not open for further replies.

ThomasBB

IS-IT--Management
Mar 9, 2010
89
DK
Hello!

I'm not the best programmer in the world - far from it, actually - but I'm making a small program to read in a text-file and output it in a specific format for the danish customs authorities.

Basic format is:

02,date,ourCompanyNumber,CountryCodeForBuyer,BuyersCompanyNumber,amount

The catch is that the BuyersCompanyNumber may exist several times in the input file, but it's only allowed to exist once in the output-file.

Example:
02,2010-10-10,12345678,US,12341234,1000
02,2010-10-10,12345678,US,12341234,5000

Those would have to be fitted together to one line with the amount being 6000. They can be present at random places in the input-file.

Each line is saved in a string-array, but I'm uncertain how I would go about checking if the buyers company-number already exists in my array - and if it does, how I would add together the amounts.

Hope I've made myself understandable - if not, please give me a shout :)

BR
Thomas
 
you should create an object that represents the row for each instance, as you read values from the source, convert it to this new object, then verify the uniqueness you require. after you have read the information you can then write to the destination file.

here is an example of a person object that requires a unique SSN.
Code:
class Person
{
   public string Name {get;set;}
   public DateTime DateOfBirth {get;set;}
   public string SSN {get;set;}

   public override void Equals(object other)
   {
       if (ReferenceEquals(this, other)) return true;
       if (ReferenceEquals(other, null)) return false;
       return SSN == other.SSN;
   }


   public override string GetHashCode()
   {
       return SSN.GetHashCode();
   }
}
Code:
var people = new List<Person>();
var source = new FileInfo(path to source file);

foreach(var line in sourceFile.ReadLines())
{
   var person = ConvertLineToPerson(line);
   if(people.Contains(person))
   {
       handle a person with the same SSN
   }
   else
   {
       people.Add(person);
   }
}

var destination = new FileInfo(path to destination file);
foreach(var person in people)
{
   var line = ConvertPersonToLine(person);
   destination.WriteLine(line);
}
you may also want to look into the FileHelpers library. This framework makes is easy and simple to read/write text files.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi Jason

Thank you for the response. I'll try looking into the classes and see if I can make them fit to my purpose.

I haven't heard of Filehelpers before. That might be worthwhile spending a bit of time on.

BR
Thomas
 
Hi again

Just a quick note, that I used a variation of your code to get it working.

Thanks for the tip!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top