How do I use the Tango LineIterator.scan()?
Robert Kosek
robert.kosek at thewickedflea.com
Tue Oct 14 05:47:07 PDT 2008
Alexander Pánek wrote:
> auto columns = line.split(" ");
> auto firstCol = columns[0];
> auto secondCol = columns[1];
> // ... that’s a tad shorter. :P
Quite so, Alexander, and that's what I switched to in my code. I
refactored what I had and switched to commas, just in case someone uses
whitespace to pad things. ;-)
And if you know what exercise 9 is on the site, you have variable input.
Which does make it a fun experience. My constructor just takes the
line and parses it, so it's quite convenient.
I wound up with:
> this(char[] line) {
> auto parts = split(line, ",");
>
> this.sku = parts[0][0];
> this.price = Integer.parse(parts[1]);
> this.hasSpecial = parts.length >= 3;
>
> if(this.hasSpecial) {
> auto parts2 = split(parts[2], " for ");
> this.s_qty = Integer.parse(parts2[0]);
> this.s_price = Integer.parse(parts2[1]);
> }
> }
And I use the LineInput object in the parent class to read and iterate
through the lines.
I must say that I like D and the Tango library. =)
Cheers,
Robert
More information about the Digitalmars-d-learn
mailing list