std.algorithm

Meta jared771 at gmail.com
Thu Nov 30 21:49:56 UTC 2017


On Thursday, 30 November 2017 at 20:49:36 UTC, flamencofantasy 
wrote:
> Hello,
>
> I have the following csv text;
>
> auto input = "Start Date,End Date,Subject,All day 
> event,Categories,Show time as
> 1/1/2018,1/1/2018,New Year's Day,TRUE,Holiday,3
> 1/15/2018,1/15/2018,\"Martin Luther King, Jr. 
> Day\",TRUE,Holiday,3
> 2/19/2018,2/19/2018,President's Day,TRUE,Holiday,3
> 5/28/2018,5/28/2018,Memorial Day,TRUE,Holiday,3
> 7/4/2018,7/4/2018,Independence Day,TRUE,Holiday,3
> 9/3/2018,9/3/2018,Labor Day,TRUE,Holiday,3
> 11/22/2018,11/22/2018,Thanksgiving Day,TRUE,Holiday,3
> 11/23/2018,11/23/2018,Day after Thanksgiving,TRUE,Holiday,3
> 12/24/2018,12/24/2018,Christmas Eve,TRUE,Holiday,3
> 12/25/2018,12/25/2018,Christmas Day,TRUE,Holiday,3";
>
> What is the most clean compact efficient/lazy way to produce a 
> range that removes the first line and then retains the second 
> and third columns of the following lines, basically producing 
> this;
>
>
> "1/1/2018,New Year's Day
> 1/15/2018,\"Martin Luther King, Jr. Day\"
> 2/19/2018,President's Day
> 5/28/2018,Memorial Day
> 7/4/2018,Independence Day
> 9/3/2018,Labor Day,TRUE
> 11/22/2018,Thanksgiving Day
> 11/23/2018,Day after Thanksgiving
> 12/24/2018,Christmas Eve
> 12/25/2018,Christmas Day"
>
> Thanks a bunch

This *almost* works:

import std.algorithm;
import std.range;
import std.stdio;
import std.string;

void main()
{
         auto input = "Start Date,End Date,Subject,All day 
event,Categories,Show time as
                 1/1/2018,1/1/2018,New Year's Day,TRUE,Holiday,3
                 1/15/2018,1/15/2018,\"Martin Luther King, Jr. 
Day\",TRUE,Holiday,3
                 2/19/2018,2/19/2018,President's Day,TRUE,Holiday,3
                 5/28/2018,5/28/2018,Memorial Day,TRUE,Holiday,3
                 7/4/2018,7/4/2018,Independence Day,TRUE,Holiday,3
                 9/3/2018,9/3/2018,Labor Day,TRUE,Holiday,3
                 11/22/2018,11/22/2018,Thanksgiving 
Day,TRUE,Holiday,3
                 11/23/2018,11/23/2018,Day after 
Thanksgiving,TRUE,Holiday,3
                 12/24/2018,12/24/2018,Christmas Eve,TRUE,Holiday,3
                 12/25/2018,12/25/2018,Christmas 
Day,TRUE,Holiday,3";

         input.lineSplitter()
                 .dropOne()
                 .map!(l =>
                         l.splitter(',')
                          .dropOne()
                          .take(2)
                          .joiner(","))
                 .each!writeln();
}

The only problem is the comma in Martin Luther King, Jr. Day, so 
that line comes out as `1/15/2018,"Martin Luther King`.


More information about the Digitalmars-d-learn mailing list