How do I use the Tango LineIterator.scan()?
Jarrett Billingsley
jarrett.billingsley at gmail.com
Mon Oct 13 10:48:30 PDT 2008
On Mon, Oct 13, 2008 at 1:33 PM, Robert Kosek
<robert.kosek at thewickedflea.com> wrote:
> Jarrett Billingsley wrote:
>>
>> LineIterator.scan is really kind of an internal method, I'm not sure
>> why it's documented.
>
>>
>>
>> There are really two ways to use a StreamIterator: the foreach way and
>> the next() way.
>
> That's really helpful, but what I'm trying to figure out how to do is grab
> data from a plain text file. Columns are delimited by spaces, but the third
> column (the last) also contains spaces. So I'm really searching for a char,
> an integer, and then a string delimited by a space.
>
> How would I go about doing this without regex? I know how to use regex, in
> a general sense, but I don't need it for something this simple.
>
> Thanks for your time Jarrett.
>
> Robert
>
In that case, once you have the line that you got from the
LineIterator, you can split it up using methods found in
tango.text.Util. You can use the locate() function to find the first
space. You can then set it to start at a certain position, so that
you can get the position of the second space after it.
import tango.text.Util;
...
// using line as the line of text.
auto firstSpace = line.locate(' '); // syntactic sugar for locate(line, ' ')
auto secondSpace = line.locate(' ', firstSpace + 1);
auto firstCol = line[0 .. firstSpace];
auto secondCol = line[firstSpace + 1 .. secondSpace];
auto thirdCol = line[secondSpace + 1 .. $]; // $ means line.length
// now you can convert the columns as you please
More information about the Digitalmars-d-learn
mailing list