Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 24, 2019 Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Hi all It's me again ;-) (because you're very strong in Dlang, here...) In want a result as i write in a comment, with remove unwanted '\r' >> import std.stdio; import std.file; import std.conv; import std.process : executeShell; /* Content of 'values.txt' >> abcd 1234 03b52h */ void main() { auto fichier = File("values.txt", "r+"); string[] buffer; foreach (line ; File("values.txt").byLine) buffer ~= line.to!string; writeln(buffer); // Result: ["abcd\r", "1234\r", "03b52h"] writeln(buffer[0], " - ", buffer[1], " - ", buffer[2]); // Result: - 03b52h :-( // Instead, i would : abcd - 1234 - 03b52h !!! executeShell("pause"); fichier.close(); } Thanks a lot, by advance :-) |
October 24, 2019 Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mil58 | On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:
> Hi all It's me again ;-)
> (because you're very strong in Dlang, here...)
> In want a result as i write in a comment, with remove unwanted '\r' >>
>
> import std.stdio;
> import std.file;
> import std.conv;
> import std.process : executeShell;
>
> /* Content of 'values.txt' >>
> abcd
> 1234
> 03b52h
> */
>
> void main() {
> auto fichier = File("values.txt", "r+");
> string[] buffer;
> foreach (line ; File("values.txt").byLine)
> buffer ~= line.to!string;
>
> writeln(buffer);
> // Result: ["abcd\r", "1234\r", "03b52h"]
>
> writeln(buffer[0], " - ", buffer[1], " - ", buffer[2]);
> // Result: - 03b52h :-(
>
> // Instead, i would : abcd - 1234 - 03b52h !!!
>
> executeShell("pause");
> fichier.close();
> }
>
> Thanks a lot, by advance :-)
void main() {
File("data.txt", "r+")
.byLineCopy()
.array()
.each!writeln;
}
byLineCopy removes the new lines. If in the future you would need the new line symbol call byLineCopy(Yes.keepTerminator)
|
October 24, 2019 Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Posted in reply to welkam | On Thursday, 24 October 2019 at 16:21:47 UTC, welkam wrote:
> On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:
>> [...]
>
> void main() {
> File("data.txt", "r+")
> .byLineCopy()
> .array()
> .each!writeln;
> }
>
> byLineCopy removes the new lines. If in the future you would need the new line symbol call byLineCopy(Yes.keepTerminator)
Sorry but not working ... Surely i'm not able to insert and adapt to my own script ! :-(
Could you, please, modify with the right syntax at the right place ? - Thanks.
|
October 24, 2019 Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mil58 | On 10/24/2019 09:49 AM, Mil58 wrote: > On Thursday, 24 October 2019 at 16:21:47 UTC, welkam wrote: >> On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote: >>> [...] >> >> void main() { >> File("data.txt", "r+") >> .byLineCopy() >> .array() >> .each!writeln; >> } >> >> byLineCopy removes the new lines. If in the future you would need the new line symbol call byLineCopy(Yes.keepTerminator) > > Sorry but not working ... Surely i'm not able to insert and adapt to my own script ! :-( > Could you, please, modify with the right syntax at the right place ? - Thanks. The following program works for me. As I'm on Linux, I had to write a makeFile() function to generate the file to ensure '\r' line endings. (Otherwise I would get '\n'.) 'result's type is string. import std.stdio; import std.file; import std.algorithm : each, joiner, map; import std.array : array, empty; import std.conv : text; import std.typecons : No; void makeFile(string fileName) { const lines = ["abcd", "1234", "03b52h"]; auto fishier = File(fileName, "w"); lines.map!(l => l ~ '\r').each!(l => fishier.write(l)); } void main() { enum fileName = "values_test.txt"; makeFile(fileName); auto lines = File(fileName, "r").byLineCopy(No.keepTerminator, '\r'); auto result = lines.joiner("-").text; writeln(result); } Ali |
October 24, 2019 Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 10/24/2019 10:21 AM, Ali Çehreli wrote: > auto lines = File(fileName, "r").byLineCopy(No.keepTerminator, '\r'); > auto result = lines.joiner("-").text; I would normally do the following auto lines = File(fileName, "r").byLineCopy(No.keepTerminator, '\r'); auto result = format!"%-(%s-%)"(lines); But I ran into the following issue: https://issues.dlang.org/show_bug.cgi?id=20317 Ali |
October 24, 2019 Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mil58 | On Thursday, 24 October 2019 at 16:49:09 UTC, Mil58 wrote:
> On Thursday, 24 October 2019 at 16:21:47 UTC, welkam wrote:
>> On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:
>>> [...]
>>
>> void main() {
>> File("data.txt", "r+")
>> .byLineCopy()
>> .array()
>> .each!writeln;
>> }
>>
>> byLineCopy removes the new lines. If in the future you would need the new line symbol call byLineCopy(Yes.keepTerminator)
>
> Sorry but not working ... Surely i'm not able to insert and adapt to my own script ! :-(
> Could you, please, modify with the right syntax at the right place ? - Thanks.
Ah sorry my bad. byLineCopy defaults to '\n' as line terminator so it worked on my system.
import std.algorithm;
File("data.txt", "r+")
.byLineCopy(No.keepTerminator, '\r')
.joiner(" - ")
.writeln;
this will print the thing you wanted. If you want to capture that value in a variable then
auto result = File("data.txt", "r+")
.byLineCopy(No.keepTerminator, '\r')
.joiner(" - ");
|
Copyright © 1999-2021 by the D Language Foundation