Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 28, 2013 readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Why is when you do readln() the newline character (\n) gets read too? Wouldn't it make more sense for that character to be stripped off? |
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote:
> Why is when you do readln() the newline character (\n) gets read too? Wouldn't it make more sense for that character to be stripped off?
I just want to add to this, that it makes it really annoying to work with the command line, as you kinda have to strip off the last character and thus cannot make the string immutable.
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | Jeroen Bollen:
> it makes it really annoying to work with the command line, as you kinda have to strip off the last character and thus cannot make the string immutable.
void main() {
import std.stdio, std.string;
immutable txt = readln.chomp;
writeln(">", txt, "<");
}
Bye,
bearophile
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 12/28/13 8:49 AM, Jeroen Bollen wrote:
> Why is when you do readln() the newline character (\n) gets read too?
> Wouldn't it make more sense for that character to be stripped off?
So you know that if it returns an empty string the file is done.
Andrei
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 12/28/13 8:50 AM, Jeroen Bollen wrote:
> On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote:
>> Why is when you do readln() the newline character (\n) gets read too?
>> Wouldn't it make more sense for that character to be stripped off?
>
> I just want to add to this, that it makes it really annoying to work
> with the command line, as you kinda have to strip off the last character
> and thus cannot make the string immutable.
Try stdin.byLine, which by default strips the newline.
Andrei
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 28 December 2013 at 17:07:23 UTC, Andrei Alexandrescu wrote:
> On 12/28/13 8:49 AM, Jeroen Bollen wrote:
>> Why is when you do readln() the newline character (\n) gets read too?
>> Wouldn't it make more sense for that character to be stripped off?
>
> So you know that if it returns an empty string the file is done.
And also so a readln/writeln loop preserves line endings.
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Saturday, 28 December 2013 at 16:50:21 UTC, Jeroen Bollen wrote:
> On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote:
>> Why is when you do readln() the newline character (\n) gets read too? Wouldn't it make more sense for that character to be stripped off?
>
> I just want to add to this, that it makes it really annoying to work with the command line, as you kinda have to strip off the last character and thus cannot make the string immutable.
It doesn't stop you from stripping off the last character. Assuming that you're using the nullary overload of `readln`: the return type is `string`, which is an alias of `immutable(char)[]`, which is a mutable slice of immutable characters:
---
void main()
{
import std.stdio;
auto line = readln();
if (line.length != 0) // Standard input had data
{
line = line[0 .. $ - 1]; // Slice off EOL
writefln(`got line: "%s"`, line);
}
}
---
Writing to the characters in `line` is not permitted as they are immutable, but slicing `line`, as well as reassigning `line` to a different slice, is perfectly fine because the slice itself is mutable. `immutable(char[])` would be the type where both the characters and the slice are immutable.
If you also wanted to strip any trailing whitespace on the line from standard input, you could use `line = line.stripRight();` - where `stripRight` is from std.string - to do both at once.
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 12/28/2013 08:50 AM, Jeroen Bollen wrote: > On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote: >> Why is when you do readln() the newline character (\n) gets read too? Because it is possible to remove but hard or expensive or even impossible (was there a newline?) to add back if needed. >> Wouldn't it make more sense for that character to be stripped off? > > I just want to add to this, that it makes it really annoying to work > with the command line, as you kinda have to strip off the last character > and thus cannot make the string immutable. It is pretty easy actually: import std.stdio; import std.string; void main() { string line = readln.chomp; } (Or with various combinations of parethesis and without UFCS. :) ) That works even when you wanted the whole string to be immutable: immutable char[] line = readln.chomp; Or, perhaps more preferably: immutable(char[]) line = readln.chomp; Ali |
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 28 December 2013 at 17:07:58 UTC, Andrei Alexandrescu wrote:
> On 12/28/13 8:50 AM, Jeroen Bollen wrote:
>> On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote:
>>> Why is when you do readln() the newline character (\n) gets read too?
>>> Wouldn't it make more sense for that character to be stripped off?
>>
>> I just want to add to this, that it makes it really annoying to work
>> with the command line, as you kinda have to strip off the last character
>> and thus cannot make the string immutable.
>
> Try stdin.byLine, which by default strips the newline.
stdin.byLine can't strip \r\n unless you specify that as the line terminator, in which case it can't split by \n.
|
December 28, 2013 Re: readln() returns new line charater | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 28 December 2013 at 16:59:51 UTC, bearophile wrote:
> void main() {
> import std.stdio, std.string;
> immutable txt = readln.chomp;
> writeln(">", txt, "<");
> }
>
>
> Bye,
> bearophile
These examples are cute, but I think in real programs it's usually important to handle `stdin` being exhausted. With `readln`, such code is prone to go into an infinite loop.
Of course in these same real programs, `byLine` is often the better choice anyway...
|
Copyright © 1999-2021 by the D Language Foundation