C#  Matching an item in a list with the corresponding item in a text file

Paulagier
Paulagier used Ask the Experts™
on
Hello experts
C#
I have a List Lnodes with just one item per line: an int value  named "IdNumber"

I have a .txt file with two items per line:
- "IdNumber" (always the same lenght and position)
- a separator (;)
- "Position" (a string item with different length)

I need to read, line by line, the List Lnodes and,  for each value of IdNumber;
             - browse the .txt file
             - find the line that contains the same IdNumber value
             - extract on that line the corresponding "Position" string
             - Add that value to a row on another list

I'v tried a lot of methods (substrings, arrays, and so on) and now I'm lost !!
Hereunder are a sample of Lnodes and another of the .text file


List Lnodes

310
311
312
313
314
....



.txt file
310;UpSpine
311;LeftArm
312;LeftForeArm
313;LeftHand
314;MidSpine
315;RightForeArm
316;RightArm
317;Pelvis
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer

Commented:
The best way is to split it based on the delimiter . . .eg the ;.

string line;
int NodeNumber;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\myFile.txt");
while((line = file.ReadLine()) != null)
{
NodeNumber = -1;
string  firstPart = line.Split(new char[]{';'})[0];
  if (int.TryParse(firstPart, out NodeNumber))
  {
   // will only happen if firstPart is parsed
   if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    } 
   }
}   

Open in new window

PaulagierResearch Director

Author

Commented:
Hello Kyle,
it seems not to be exactly what I'm looking for: i need to read first the List and see if its IdNumber value has an equivalent in the .txt files.
In the solution you wrote, if a NodeNumber does not match with the text file I can't know it
regards
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Paulagier;

Try this out, it should give you what you need.
// File location on disk
string filename = @"C:\Working Directory\data.txt";
// You list of Lnodes
List<int> Lnodes = new List<int>() { 317, 311, 312, 313, 314, 315, 316, 310 };
// Read the text file into memory
List<string> txtFile = File.ReadAllLines(filename).ToList();
// All the position data is in this List in the same order as Lnodes
List<string> position;

position = (from id in Lnodes
            join pos in txtFile on id.ToString() equals pos.Substring(0, 3)
            select pos.Substring(4)).ToList();

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Kyle AbrahamsSenior .Net Developer

Commented:
can you write the full example of what your'e trying to do . . . you can know it by adding an else but your requirements aren't clear to me.

  if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    }
  else
   {
   //Not in LNOdes . . . do something else?
  }
PaulagierResearch Director

Author

Commented:
if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    }
  else
   {
   //Not in LNOdes . . . do something else?
  }

The problem comes if an IdNumber value is contained in LNodes and NOT in the .txt file. It seems to me that in your solution i cannot handle that situation.
Let's add in my example the LNodes value "321" , everything else equal.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
What do you need to happen when a Lnodes value is NOT in the text file?

Did you try my solution?
PaulagierResearch Director

Author

Commented:
Dear Fernando
I nned to store somewhere (list, .txt file..)  the IdNumber that is not in the text file
Fernando SotoRetired
Distinguished Expert 2017

Commented:
So are you saying that you only need the Lnodes numbers  that are not in the text file and not a list of a position that are in the file?

If that is not the case, given the two input info, List of Lnodes and the text file, please show with values what you want the resulting object should look like.
Kyle AbrahamsSenior .Net Developer

Commented:
where are you storing them?

if (!Lnodes.Contains(NodeNumber))
   {
 
    //store the NodeNumber
  }
PaulagierResearch Director

Author

Commented:
List Lnodes            .txt file
310                           310;UpSpine
311                           311;LeftArm
312                          312;LeftForeArm
313                          313;LeftHand
314                          314;MidSpine
320                          315;RightForeArm
                           316;RightArm
                           317;Pelvis

I need the three subsets:
In LNodes and in .txt file:                 310,311,312,313,314
In LNodes and NOT in .txt file:        320
In .txt file and NOT in vNode:          315,316,317
Retired
Distinguished Expert 2017
Commented:
Here you go Paulagier as per your most recent specifications.
// File location on disk
string filename = @"C:\Working Directory\data.txt";
// You list of Lnodes
List<int> Lnodes = new List<int>() { 310, 311, 312, 313, 314, 320 };
// Read the text file into memory
List<string> txtFile = File.ReadAllLines(filename).ToList();
// Three result sets 1: In LNodes and in .txt file, 2: In LNodes and NOT in .txt file and 3: In .txt file and NOT in vNode
List<string> inLnodesAndFile;
List<string> inLnodesNotFile;
List<string> inFileNotLnodes;

// Redefined the input to be in the same data type and cut down on typing
List<string> LnAsString = Lnodes.Select(l => l.ToString()).ToList();
List<string> fileIds = txtFile.Select(fi => fi.Substring(0, 3)).ToList();

// The requested data
inLnodesAndFile = LnAsString.Intersect(fileIds).ToList();
inLnodesNotFile = LnAsString.Except(fileIds).ToList();
inFileNotLnodes = fileIds.Except(LnAsString).ToList();

Open in new window

PaulagierResearch Director

Author

Commented:
A GREAT solution,  with a LOT of expertise-behind
Thanks Fernando

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial