-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?

A--C
  • 36,351
  • 10
  • 106
  • 92
deanson
  • 9
  • 1
  • 1
  • 2
  • 8
    I smell homework... – Hanlet Escaño Feb 28 '13 at 23:28
  • What have you tried? What approach do you think would get the results your looking for? – Tim Feb 28 '13 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`? – Brian Feb 28 '13 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<>` – MethodMan Feb 28 '13 at 23:33
  • KoreyLegend, what have you tried and where you stuck? – Sergey Berezovskiy Feb 28 '13 at 23:34
  • Not Homework haha am 25, and its for a program to track and monitor performance (running) I have tried to split the array with \r\n to get each line and then split the lines with \t to get an individual data representative but am struggling to find a way to go through each line and put 84 in top_speed array for example then 73 in average_speed and so on Thanks – deanson Feb 28 '13 at 23:36
  • `-1` age has no bearing on School or learning by the way `KoreyLegend` – MethodMan Feb 28 '13 at 23:42
  • technically it does would you expect a 2 month baby attend school and have commitments such as homework? That is age dependent. Why cant people just share their knowledge and not write pointless info – deanson Feb 28 '13 at 23:44
  • @KoreyLegend we can, but thats node *write code for my task* site - thats *help me with my code* site :) – Sergey Berezovskiy Feb 28 '13 at 23:45
  • @KoreyLegend: Saying "I tried to do it" without showing what you tried is meaningless, and insulting people you're asking to provide you with free help to solve your problems is seldom a good idea. Please take a few minutes to read the [FAQ](http://stackoverflow.com/faq), particularly the sections on how to ask better questions and appropriate conduct. Referring to comments made in an attempt to help you improve your question as "pointless" is rude. This is not a code writing service; we expect demonstrated effort to solve the problem yourself first. Thanks. – Ken White Feb 28 '13 at 23:45
  • Am sorry but at no point did i ask anyone to do it for me, i asked whats the best way to achieve this? a break down perhaps – deanson Feb 28 '13 at 23:47

2 Answers2

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();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
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);
itsme86
  • 19,266
  • 4
  • 41
  • 57