splitting numbers from a test file

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 18 21:04:31 PDT 2012


On Wednesday, September 19, 2012 05:36:36 Craig Dillabaugh wrote:
> Thanks, a few others have pointed that out to me too.  But as a D
> newbie how would I have any clue what splitter returns since the
> return type is auto?

The documentation says that it returns a range. Presumably then, the problem 
is that you're not familiar with ranges, and that needs to be handled better. 
We really need a proper article/tutorial on the main site which explains them, 
and we don't. But I don't know what we'd do differently in the documentation 
for functions in general. Ranges are a concept that are used quite heavily in 
Phobos, and it wouldn't make sense to try and explain them for every function 
that uses them.

> The is an example in the docs.
> 
> auto a = " a     bcd   ef gh ";
> assert(equal(splitter(a), ["", "a", "bcd", "ef", "gh"][]));

It would have used == if it were an array. equal operates on ranges, so if 
it's used, odds are that the types on the right and left sides are different.

> I guessed that since the return of splitter was equal to :
> ["", "a", "bcd", "ef", "gh"][]
> it was returning some sort of 2D array!
> 
> When a function returns an 'auto' in the Phobos is this generally
> indicative of the return value being a range?

That's the most common, but it's not always the case. It will usually say in 
the documentation though (and if you're familiar with ranges, it's generally 
fairly obvious if the return type is a range just based on what the function 
is doing), and in this case it does.

> I think my problem was that I was trying to call strip on it first
> to remove leading/trailing whitespace and I was getting syntax
> errors when I called strip() on the char[]. Just calling split works as
> you say.

strip works just fine on a char[]. I don't know why you were having problems 
with it. Maybe you're using an older release of the compiler and strip used to 
take a string rather than being templated on character type? I don't know. If 
you're on 2.060 though, strip should work just fine with char[].

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list