www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Remove unwanted ' \r ' from an Array ... by reading a *.txt file.

reply Mil58 <peter_pinkness yahoo.fr> writes:
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 :-)
Oct 24 2019
parent reply welkam <wwwelkam gmail.com> writes:
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)
Oct 24 2019
parent reply Mil58 <peter_pinkness yahoo.fr> writes:
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.
Oct 24 2019
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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() { =C2=A0=C2=A0=C2=A0 File("data.txt", "r+") =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 .byLineCopy() =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 .array() =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 .each!writeln; } byLineCopy removes the new lines. If in the future you would need the =
 new line symbol call byLineCopy(Yes.keepTerminator)
=20 Sorry but not working ... Surely i'm not able to insert and adapt to my=
=20
 own script !=C2=A0 :-(
 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=20 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 =3D ["abcd", "1234", "03b52h"]; auto fishier =3D File(fileName, "w"); lines.map!(l =3D> l ~ '\r').each!(l =3D> fishier.write(l)); } void main() { enum fileName =3D "values_test.txt"; makeFile(fileName); auto lines =3D File(fileName, "r").byLineCopy(No.keepTerminator, '\r')= ; auto result =3D lines.joiner("-").text; writeln(result); } Ali
Oct 24 2019
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/24/2019 10:21 AM, Ali =C3=87ehreli wrote:
  =C2=A0 auto lines =3D File(fileName, "r").byLineCopy(No.keepTerminator=
, '\r');
  =C2=A0 auto result =3D lines.joiner("-").text;
I would normally do the following auto lines =3D File(fileName, "r").byLineCopy(No.keepTerminator, '\r')= ; auto result =3D format!"%-(%s-%)"(lines); But I ran into the following issue: https://issues.dlang.org/show_bug.cgi?id=3D20317 Ali
Oct 24 2019
prev sibling parent welkam <wwwelkam gmail.com> writes:
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(" - ");
Oct 24 2019