Baffled by compilation error for formattedRead
Dennis Ritchie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 7 17:04:20 PDT 2015
On Thursday, 7 May 2015 at 23:13:41 UTC, PhilipDaniels wrote:
> On Thursday, 7 May 2015 at 23:10:26 UTC, PhilipDaniels wrote:
>
> Let's try reformatting that...
>
> ubyte r, g, b;
>
> // does not compile
> auto numRead = formattedRead(dropExactly(input, 4), "%x/%x/%x",
> &r, &g, &b);
> // does not compile
> auto numRead = formattedRead(input[4..$], "%x/%x/%x", &r, &g,
> &b);
>
> // compiles
> string s2 = input[4..$];
> auto numRead = formattedRead(s2, "%x/%x/%x", &r, &g, &b);
Alternatively, I can suggest to use the function csvReader():
-----
import std.csv,
std.stdio,
std.format;
void main() {
auto input = "rgb:10/30/40";
input = input[4 .. $];
ubyte r, g, b;
formattedRead(input, "%s/%s/%s", &r, &g, &b);
auto numRead = [r, g, b];
string[] numReadHex;
foreach (e; numRead) {
numReadHex ~= format("%x", e);
}
writeln(numRead); // [10, 30, 40]
writeln(numReadHex); // ["a", "1e", "28"]
auto inputNew = "rgb:10/30/40";
auto newNumRead = csvReader!int(inputNew[4 .. $], '/').front;
writeln(newNumRead); // [10, 30, 40]
}
More information about the Digitalmars-d-learn
mailing list