Hardik Dhankecha
Hardik Dhankecha

Reputation: 148

How to collect array of matched value from string?

I am filling data from memory mapped file to string like :

AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984

AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464

and so on...

So now I want to make an array of close value of all days. And there are collection of thousands of days data in memory mapped file and string. So how can I fetch close value and can make array of its?

I am trying to make it's array but it's make whole data into single array. So it's not what i want.

string[] lines = System.IO.File.ReadAllLines(@"D:\mine.txt");
foreach (string line in lines)
{
    // Use a tab to indent each line of the file.
    Console.WriteLine("\t" + line);
}

byte[] bytes = new byte[10000000];
stream.ReadArray(0, bytes, 0, bytes.Length);
string txt = Encoding.UTF8.GetString(bytes).Trim('\0');`

So I need an array of all close value to fetch from that string. Like that:

{78.433, 77.442, etc..}

Upvotes: 0

Views: 128

Answers (4)

Michał Turczyn
Michał Turczyn

Reputation: 37347

Try this:

File.ReadLines(@"D:\mine.txt")
  // Pick only those lines starting with "Close"
  .Where(line => line.StartsWith("Close:"))
  // Get value, which follows colon, and parse it do double
  .Select(line => double.Parse(line.Split(':')[1]))
  // Convert result to an array
  .ToArray();

Upvotes: 1

jdweng
jdweng

Reputation: 34421

Try following which accepts blank lines :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication98
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            AAPL aapl = new AAPL(FILENAME);
        }
    }
    public class AAPL
    {
        static List<AAPL> aapls = new List<AAPL>();
        private DateTime date { get; set; }
        public decimal open { get; set; }
        public decimal close { get; set; }
        public decimal low { get; set; }
        public decimal high { get; set; }
        public int volume { get; set; }

        public AAPL() { }
        public AAPL(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            string line = "";
            AAPL newAAPL = null;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();

                if (line.Length > 0)
                {
                    if (line.StartsWith("AAPL"))
                    {
                        string dateStr = line.Substring(line.IndexOf(",") + 1);
                        date = DateTime.Parse(dateStr);
                        newAAPL = new AAPL();
                        aapls.Add(newAAPL);
                        newAAPL.date = date;
                    }
                    else
                    {
                        string[] splitArray = line.Split(new char[] { ':' });

                        switch (splitArray[0])
                        {
                            case "Open":
                                newAAPL.open = decimal.Parse(splitArray[1]);
                                break;

                            case "Close":
                                newAAPL.close = decimal.Parse(splitArray[1]);
                                break;

                            case "Low":
                                newAAPL.low = decimal.Parse(splitArray[1]);
                                break;

                            case "High":
                                newAAPL.high = decimal.Parse(splitArray[1]);
                                break;

                            case "Volume":
                                newAAPL.volume = int.Parse(splitArray[1]);
                                break;
                        }
                    }
                }
            }
        }


    }
}

Upvotes: -1

Amine
Amine

Reputation: 142

I supposed your file Like this :

AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984
AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464

Try this

   var lines = System.IO.File.ReadAllLines(@"C:\Users\bouyami\Documents\AB_ATELIER\1.txt").ToList();
        var linesFiltred = lines.Where(x => x.StartsWith("Close")).ToList();
        var result = linesFiltred.Select(x => x.Split(':')[1]).ToList();

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117027

Try this:

decimal[] arrayOfCloses =
    File
        .ReadAllLines(@"D:\mine.txt")
        .Select(x => x.Split(':'))
        .Where(x => x.Length == 2)
        .Where(x => x[0] == "Close")
        .Select(x => decimal.Parse(x[1]))
        .ToArray();

Upvotes: 3

Related Questions