Jump to page: 1 2 3
Thread overview
readln() returns new line charater
Dec 28, 2013
Jeroen Bollen
Dec 28, 2013
Jeroen Bollen
Dec 28, 2013
bearophile
Dec 28, 2013
Jakob Ovrum
Dec 28, 2013
Jeroen Bollen
Dec 28, 2013
Jakob Ovrum
Dec 29, 2013
Jeroen Bollen
Dec 29, 2013
Jakob Ovrum
Dec 29, 2013
Jeroen Bollen
Dec 30, 2013
Marco Leise
Dec 31, 2013
Jeroen Bollen
Dec 31, 2013
Marco Leise
Dec 31, 2013
Stewart Gordon
Dec 31, 2013
Marco Leise
Dec 31, 2013
bearophile
Dec 30, 2013
Regan Heath
Dec 30, 2013
Jakob Ovrum
Dec 30, 2013
Regan Heath
Dec 28, 2013
Vladimir Panteleev
Dec 29, 2013
Dmitry Olshansky
Dec 29, 2013
Vladimir Panteleev
Dec 29, 2013
Dmitry Olshansky
Dec 28, 2013
Jakob Ovrum
Dec 28, 2013
Ali Çehreli
Dec 28, 2013
Vladimir Panteleev
Dec 30, 2013
Marco Leise
Dec 30, 2013
Vladimir Panteleev
Dec 31, 2013
Stewart Gordon
December 28, 2013
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
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
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
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
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
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
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
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
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
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...
« First   ‹ Prev
1 2 3