-4

How can I loop through a text file to create six arrays from the content of the text file. For example, the text file will look like this but with more lines(without title) maybe 200 of them

top_speed average_speed cadence altitude heart_rate power

       84            73       0     -124          0    50
       86           179       84    -125        121  3893

It would be nice to have an array for each. So, for example

top_speed = 84 + 86 : average_speed = 73 + 179 ... (and so on)

What's the best way to do this?

11
  • 8
    I smell homework... Commented Feb 28, 2013 at 23:28
  • What have you tried? What approach do you think would get the results your looking for? Commented Feb 28, 2013 at 23:30
  • This is not only easy to do, but easily searched for with Google. Are you looking to make a Console app, Winforms, web page? Commented Feb 28, 2013 at 23:31
  • 2
    -1 KoreyLegend for severe lack of effort here is a hint lookup how to use the Split() method and how to ReadAllLines() into a List<> Commented Feb 28, 2013 at 23:33
  • KoreyLegend, what have you tried and where you stuck? Commented Feb 28, 2013 at 23:34

2 Answers 2

0

Anyway, if that is homework, following code will not help you :) But if it is not homework, you will understand how to parse such files with LINQ

var items = 
   File.ReadAllLines(filename) // read lines from file
       .Select(line => line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                           .Select(Int32.Parse) 
                           .ToArray()) // convert each line to array of integers
       .Select(values => new {
            TopSpeed = values[0],
            AverageSpeed = values[1],
            Cadence = values[2],
            Altitude = values[3],
            HeartRate = values[4],
            Power = values[5]
       }); // create anonymous object with nice strongly-typed properties

int[] topSpeeds = items.Select(i => i.TopSpeed).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0

You could create a Record class and then use a simple LINQ query:

var records = File.ReadLines("file.txt")
    .Select(line =>
        {
            string[] parts = line.Split('\t');
            return new Record
                {
                    TopSpeed = int.Parse(parts[0]),
                    AverageSpeed = int.Parse(parts[1]),
                    Cadence = int.Parse(parts[2]),
                    Altitude = int.Parse(parts[3]),
                    HeartRate = int.Parse(parts[4]),
                    Power = int.Parse(parts[5])
                };
        }).ToArray();

This will give you a bunch of Records, one per line in the original file. If you wanted to then check all of HeartRates for building a histogram or graphing or whatever, you could grab them like this:

var allHeartRates = records.Select(rec => rec.HeartRate);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.