www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Selected elements from splitter output

reply Chris Piker <chris hoopjump.com> writes:
Hi D

I have a white-space delimited file with quite a few columns, but 
I only care about columns 0, 2, 3, 4, 8, 9, 10.  Since I don't 
need most of the 60+ columns it seemed like:

    std.algorithm.iteration.splitter()

would be a better function to use then std.array.split().  My 
problem is that I don't know how to get the elements I care about 
from the splitter, for example:

char[] line;
char[][] cols_needed;
while(file.readln(line)){
    auto a = line.splitter()
    cols_needed = ???
}

On a related note, are there any standard library functions that 
select specific elements of a range by index without a loop?  So 
the logical equivalent of:

auto string_range
char[][] wanted = string_range.get( [1, 5, 7] );  // pseudo-code 
element selection

It's not a big deal if there's not something standard.

Thanks for the help,
May 04 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/4/21 1:40 PM, Chris Piker wrote:

 I only care about columns 0, 2, 3, 4, 8, 9, 10.
That's std.range.stride.
 char[][] wanted = string_range.get( [1, 5, 7] );  // pseudo-code element
That's std.range.indexed. import std.range; import std.stdio; void main() { auto r = 10.iota.stride(2); writeln(r); writeln(r.indexed([1, 3])); // Note: The above works only because 'stride' applies // "design by introspection" (DbI) and is able to work as a // RandomAccessRanges. Not every range can do that; so, in a more // general case, you would have to turn your range to a // RandomAccessRange by calling std.array.array first: auto r2 = r.array; // The following can work with any InputRange only after doing that. writeln(r2.indexed([1, 3])); } Ali
May 04 2021
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/4/21 3:02 PM, Ali =C3=87ehreli wrote:

    // Note: The above works only because 'stride' applies
    // "design by introspection" (DbI) and is able to work as a
    // RandomAccessRanges.
Ok, I was too enthusiastic there. The RandomAccessRange'ness of the=20 input range changes how efficient stride() works but we can't say DbI is = used there. Ali
May 04 2021
prev sibling parent Chris Piker <chris hoopjump.com> writes:
On Tuesday, 4 May 2021 at 22:02:11 UTC, Ali Çehreli wrote:
 On 5/4/21 1:40 PM, Chris Piker wrote:

 I only care about columns 0, 2, 3, 4, 8, 9, 10.
That's std.range.stride.
 char[][] wanted = string_range.get( [1, 5, 7] );  //
pseudo-code element That's std.range.indexed.
Hey Thanks! And even more, thanks for the book. It's very well organized. I keep a paper copy of "Programming in D" open on my desk all the time these days. Looking forward to getting a few copies for work once we're back in the office.
May 04 2021