digitalmars.D.learn - Absolute beginner
- Jorge (5/5) Jan 13 2012 My first question si very silly:
- Piotr Szturmaj (4/8) Jan 13 2012 import std.conv;
- Jorge (25/25) Jan 13 2012 Thanks for your answer but:
- Timon Gehr (13/38) Jan 13 2012 readln() includes the trailing newline character in the resulting
- bearophile (6/11) Jan 13 2012 123
- Matej Nanut (5/16) Jan 13 2012 While we're at it: what's the best way to parse in a formatted manner?
- Justin Whear (7/13) Jan 13 2012 Check out readf: http://d-programming-language.org/phobos/
- Matej Nanut (5/18) Jan 14 2012 I've never noticed std.conv.parse takes a radix argument, silly me.
- H. S. Teoh (30/35) Jan 13 2012 [...]
- Joshua Reusch (6/17) Jan 13 2012 the string returned by readln() ends with NL ('\n'), which causes
- Jorge (1/1) Jan 13 2012 Ok, it works fine. Thx to you all ;-) and sorry for my noob question.
- Matej Nanut (4/27) Jan 13 2012 You could also try to!int(str.strip), strip() removes
My first question si very silly: string str = readln() my input is for example 123 how can i convert this to an integer? Thanks.
Jan 13 2012
Jorge wrote:My first question si very silly: string str = readln() my input is for example 123 how can i convert this to an integer?import std.conv; // then in code: auto i = to!int(str);
Jan 13 2012
Thanks for your answer but: import std.stdio; import std.conv; void main() { write("Insert number: "); string s = readln(); auto i = to!int(s); } compiles but after i enter a number and press the enter key i get: std.conv.ConvException .\..\..\src\phobos\std\conv.d(1595): Can't convert value ` ' of type string to type int ---------------- 41DECC 41DD43 4027F3 402475 402D6C 402DB0 4029A7 4BBA79 ---------------- what's wrong?
Jan 13 2012
On 01/13/2012 10:34 PM, Jorge wrote:Thanks for your answer but: import std.stdio; import std.conv; void main() { write("Insert number: "); string s = readln(); auto i = to!int(s); } compiles but after i enter a number and press the enter key i get: std.conv.ConvException .\..\..\src\phobos\std\conv.d(1595): Can't convert value ` ' of type string to type int ---------------- 41DECC 41DD43 4027F3 402475 402D6C 402DB0 4029A7 4BBA79 ---------------- what's wrong?readln() includes the trailing newline character in the resulting string. You can use std.string.strip to remove leading and trailing whitespace: import std.stdio; import std.conv; import std.string; void main() { write("Insert number: "); string s = readln(); auto i = to!int(strip(s)); }
Jan 13 2012
Timon Gehr:readln() includes the trailing newline character in the resulting string. You can use std.string.strip to remove leading and trailing whitespace:Time ago I have asked Andrei to modify the to!int conversion to work as Python, ignoring leading and trailing whitespace:123 But he didn't change it. I don't think people use parse at their first try. Bye, bearophiles = "123\n" int(s)
Jan 13 2012
While we're at it: what's the best way to parse in a formatted manner? For example, if I want to get 5 hexadecimal digits converted into an uint? And I want to simultaneously advance the string? "sscanf" seems fiddly and unsafe. On 13 January 2012 22:56, bearophile <bearophileHUGS lycos.com> wrote:Timon Gehr:readln() includes the trailing newline character in the resulting string. You can use std.string.strip to remove leading and trailing whitespace:Time ago I have asked Andrei to modify the to!int conversion to work as Python, ignoring leading and trailing whitespace:123 But he didn't change it. I don't think people use parse at their first try. Bye, bearophiles = "123\n" int(s)
Jan 13 2012
On Fri, 13 Jan 2012 23:05:19 +0100, Matej Nanut wrote:While we're at it: what's the best way to parse in a formatted manner? For example, if I want to get 5 hexadecimal digits converted into an uint? And I want to simultaneously advance the string? "sscanf" seems fiddly and unsafe.Check out readf: http://d-programming-language.org/phobos/ std_stream.html#readf You'll need to wrap the string in a Stream interface (MemoryStream?), but since you're looking for advance-on-read, you want stream semantics anyhow. Justin
Jan 13 2012
I've never noticed std.conv.parse takes a radix argument, silly me. And will take a look at readf from std.stream, definitely. Thanks! On 14 January 2012 01:20, Justin Whear <justin economicmodeling.com> wrote:On Fri, 13 Jan 2012 23:05:19 +0100, Matej Nanut wrote:While we're at it: what's the best way to parse in a formatted manner? For example, if I want to get 5 hexadecimal digits converted into an uint? And I want to simultaneously advance the string? "sscanf" seems fiddly and unsafe.Check out readf: http://d-programming-language.org/phobos/ std_stream.html#readf You'll need to wrap the string in a Stream interface (MemoryStream?), but since you're looking for advance-on-read, you want stream semantics anyhow. Justin
Jan 14 2012
On Fri, Jan 13, 2012 at 11:05:19PM +0100, Matej Nanut wrote:While we're at it: what's the best way to parse in a formatted manner? For example, if I want to get 5 hexadecimal digits converted into an uint?[...] Have you tried: import std.conv; ... uint val = parse!uint(input_string[0..5], 16); ?And I want to simultaneously advance the string?You can always keep track of how many characters were consumed by parse() and advancing the string yourself. Something like this: import std.conv; string tmp = input_string[0..5]; uint val = parse!uint(tmp, 16); input_string = input_string[5-tmp.length .. $]; If you find this a bit verbose, you can always encapsulate it in a function and use that instead."sscanf" seems fiddly and unsafe.IMNSHO, sscanf is one of the poorest designed functions we had the misfortune to inherit from the C library. It pretends to mirror printf() in its format string, but the semantics are subtly different and likes to come back to bite you unexpectedly at the most inopportune times (if you ever tried using "%s" to read back a string printed using "%s", or tried to count embedded spaces in the input, you'll know what I mean). In C and C++ (can't speak for the D wrappers for it) it also suffers from potential buffer overruns, pointer bugs, and all sorts of other lovely things. It seems so nice and simple to use, but comes with all sorts of nasty gotcha's, bad corner cases, etc.. I say, avoid sscanf like the plague. There are many other much better alternatives. T -- Klein bottle for rent ... inquire within. -- Stephen Mulraney
Jan 13 2012
Am 13.01.2012 22:16, Piotr Szturmaj wrote:Jorge wrote:the string returned by readln() ends with NL ('\n'), which causes std.conv.to to raise an ConvException. You also must slice it away: auto i = to!int(str[0..$-1]); or you use std.conv.parse, which ignores the characters after the value and removes the converted characters from the string.My first question si very silly: string str = readln() my input is for example 123 how can i convert this to an integer?import std.conv; // then in code: auto i = to!int(str);
Jan 13 2012
Ok, it works fine. Thx to you all ;-) and sorry for my noob question.
Jan 13 2012
You could also try to!int(str.strip), strip() removes whitespace from the left and right of a string. You have to import std.string for it. On 13 January 2012 22:34, Joshua Reusch <yoschi arkandos.de> wrote:Am 13.01.2012 22:16, Piotr Szturmaj wrote:Jorge wrote:the string returned by readln() ends with NL ('\n'), which causes std.conv.to to raise an ConvException. You also must slice it away: auto i = to!int(str[0..$-1]); or you use std.conv.parse, which ignores the characters after the value and removes the converted characters from the string.My first question si very silly: string str = readln() my input is for example 123 how can i convert this to an integer?import std.conv; // then in code: auto i = to!int(str);
Jan 13 2012