Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Where have you been all my life! I found the answer I needed in seconds..."

Geography

Where in the world do Tek-Tips members come from?

Check stringarray (list) for existing value

ThomasBB (IS/IT--Management)
23 Aug 10 4:09
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
jmeckley (Programmer)
23 Aug 10 8:53
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: Database Connection Management
FAQ732-7259: Keeping the UI responsive

ThomasBB (IS/IT--Management)
24 Aug 10 2:20
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
ThomasBB (IS/IT--Management)
25 Aug 10 13:59
Hi again

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

Thanks for the tip!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close