Jon
Jon

Reputation: 40032

Funky LINQ Required

The below is a CSV string I am working with. Its much bigger in reality but this is enough to display a pattern.

Please note that I have put this CSV on seperate lines just to demonstrate my pattern easily.

After a CSV split the number of fields are variable depending on how big the original string is i.e. the string is a variable length which makes the number of indexes variable

The letter in the pattern may not always be P, it could be U, O or F

G9999999990001800002777107050,
G9999999990002777107HMNLAQKPRLLHRAQRWJ010,
1,
3,
29,
P,
6.74,
11.23,
07,
U,
5.25,
14.29,
08,
O,
6.89,
16.92,
07,
P,
5,
4,

I want to pick up the 5th (29) and 6th (P) elements and then miss 2 elements and then pick the next element (07) and the one after (P) and so on until I get to the end of the string.

In this example I will have 29 P 07 P 08 P 07 P

Is there an easy way to do this, I assume LINQ will offer something

Thanks

Upvotes: 1

Views: 235

Answers (5)

Marc
Marc

Reputation: 4813

To parse a row I would use a RegEx instead Linq as it is compiled and much faster.

Upvotes: 0

sehe
sehe

Reputation: 393164

A full demo on http://ideone.com/EDof0

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static IEnumerable<int> SpecialIndexes()
    {
        int i=4; 

        while (i<Int32.MaxValue)
        {
            yield return i++;
            yield return i++;
            i += 2;
        }
    }

    public static void Main(string[] args)
    {
        var csvString = "G9999999990001800002777107050,G9999999990002777107HMNLAQKPRLLHRAQRWJ010,1,3,29,P,6.74,11.23,07,P,5.25,14.29,08,P,6.89,16.92,07,P,5,4,";

        var fields = csvString.Split(',');
        var selected = SpecialIndexes()
            .TakeWhile(i => i<fields.Length)
            .Select(i => fields[i]);

        Console.WriteLine(string.Join(" ", selected.ToArray()));
    }
}

Output:

29 P 07 P 08 P 07 P

Upvotes: 2

kyjan
kyjan

Reputation: 321

i've done something like this yesterday... here's my code

//Create a new variable of string
var x = string.Empty;
//Open a new FileStream
using (var fs = new FileStream(path,...))
//Open the StreamReader
using (var sr = new StreamReader(fs))
{
//Read out the whole CSV
   x = sr.ReadToEnd();
}

//Split up the string by ',' and whitespaces. dismiss empty entries
var stringArray = x.Split(new char[] { ',', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)

//stolen code following 
var myEnumerable = stringArray.Skip(2)
                              .Where((item, index) => index % 4 == 3 || index % 4 == 0)
                              .Skip(1);

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68697

line.Split(',')  //split on commas as it seems from your question that's your input
    .Skip(2) //skip the first two entries
    .Where((l, i) => i % 4 == 3 || i % 4 == 0) //take every 3rd and 4th item
    .Skip(1); //skip the first item since the index is divisible by 4

But this doesn't at all seem descriptive of what the code is doing, I'd at least put a comment.

Upvotes: 4

Amy B
Amy B

Reputation: 110121

string[] thestrings = source.Split(',');
string lastItem = thestrings.FirstOrDefault();
List<string> keepers = new List<string>();

foreach(string item in thestrings)
{
  if (item == "P")
  {
    if (lastItem != "P")
    {
      keepers.Add(lastItem);
    }
    keepers.Add(item);
  }
  lastItem = item;
}

Upvotes: 0

Related Questions