Stuck with std.algorithm.splitter

Jonathan M Davis jmdavisProg at gmx.com
Sun Apr 3 17:30:31 PDT 2011


On 2011-04-03 17:19, Aleksandar Ružičić wrote:
> I definitely must sit down and read about ranges, as it seems I don't
> quite get what exactly they are (and what types of ranges exists and
> the difference between them)..
> 
> > auto parts = array(splitter(input, '|'));
> 
> That's a nice solution! I wasn't aware of array() though..
> But I would like to avoid coping the data in this situation (I'm just
> interested in reading first one or two elements) so I'll be using
> explode() function I posted in previous email.

So, do

auto parts = splitter(input, '|');
auto first = parts.front;
parts.popFront();
auto second = parts.front;

Now, I don't think that array is doing much of the way of copying anyway, 
since the result is an array of strings, and arrays are effectively reference 
types (so it's not like the whole string is getting copied), but if all you 
want to do is take the first couple of elements, then use the proper range 
functions for it. front accesses the first element of a range, and popFront 
removes the first element from the range.

If you want to read about ranges, the original article is here:
http://www.informit.com/articles/printerfriendly.aspx?p=1407357

It's not really D-specific though. I am currently work on an article on ranges 
in D (which I guess falls under Walter's article contest, though I started it 
before his announcement), but I don't know when it will be done (well before 
the contest's deadline of June 1st, but I don't know how soon), so maybe that 
will help you when that's done. In the meantime, the original article should 
give you a good, basic understanding of ranges, and if you look at the 
documentation for std.range and std.algorithm, hopefully that will help better 
understand how they're used.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list