Add txt file to a double list?

Celso Calomeno

New member
Joined
Oct 13, 2022
Messages
3
Programming Experience
Beginner
If anyone can help me, I'm not able to convert the txt to include in the list
C#:
string thePath = @"C:\Spectrum.txt";

  string seq = File.ReadAllText(thePath);

//read the numbers by line

     string[] text = Regex.Split(seq, "\t");

List<(double mz, double intensity)> ions = new List<(double mz, double intensity)>();

  foreach (var a in ions)

      {
        //string to double conversion

           ions.Add(double.Parse(text[0]), double.Parse(text[1]));

              Console.WriteLine(a)
       }

Ex. I need my txt file to stay on my lists this format:
C#:
116.8722915649414 mz
3017.10400390625 intensity
 
Last edited by a moderator:
The first issue is that your comment on line 5 is to read the numbers by line, but you are reading all of the text in as a single string on line 3, and then splitting that string by tabs (rather than line breaks) on line 7. To make matters a ever worse you are using a regular expression to split the string when the regular String.Split() would have sufficed.

Then it looks like you are trying to put the two values into a list that holds tuples of doubles, and those tuples are the two values you are interested in. Your foreach loop starting on line 11 won't do anything because the list that you created on line 9 is empty.

Do you really need a list of tuples?

If you just need those two values do the following:
1) Open the file.
2) Read a line.
3) Split the line.
4) Parse the value that you need.
5) Store that value into a variable named mz.
6) Read a line.
7) Split the line.
8) Parse the value that you need.
9) Store that value into a variable named intensity.
 
Last edited:
Looking at your post in the Portuguese StackOverflow apparently your data format looks like this:
C#:
116.8722915649414 mz     3017.10400390625 intensity
146.2775421142578 mz     2745.87646484375 intensity
...

And not like the format that you presented in post #1.

So you do need a list. Presumably you also really want at tuple of doubles instead of using a struct or class to hold the values. In that case the pseudo code would be more like:
C#:
open the file
while (true)
{
    read a line
    if no more lines break out of loop
    split line by whitespace (e.g space, tabs, linebreaks)
    double mz = parse the 1st item in the split
    double intensity = parse the 3rd item in the split
    add (mz, intensitiy) as a tuple into the list
}
 
This I am working with a txt file with many numbers, I need to convert and insert so that when opening the list it is in the order of mz and intensity.
I'm starting in C# so I still have some language difficulties.
 

Attachments

  • Spectrum.txt
    1.8 KB · Views: 11
That spectrum.txt that you attached in post #4 has a different format. Each line there has only two numbers without the text "mz" or "intensity". In that case just change the pseudo code from my post #3 to parse the 1st and 2nd numbers on each line.

We are not a code writing service. We will not do your homework for you. Post your code. Tell us what problem you are currently encountering. We'll try to guide you towards a solution.
 
Back
Top Bottom