I am confused about how Read
separates elements.
The following all work fine:
str = StringToStream["123 xyz 456"];
Read[str, {Number, Word, Number}, RecordSeparators -> {" ", "\t"}]
Close[str];
(* {123, "xyz", 456} *)
str = StringToStream["asd, xyz"];
Read[str, {Word, Word}, WordSeparators -> {" ", "\t", ","}]
Close[str];
(* {"asd", "xyz"} *)
str = StringToStream["123xyz"];
Read[str, Number]
Close[str];
(* 123 *)
Why does this not work? And how would I handle this input with Read
, without reading in a full line as a string and post-processing it? Is it at all possible?
str = StringToStream["123, xyz, 456"];
Read[str, {Number, Word, Number}, WordSeparators -> {" ", "\t", ","}]
Close[str];
During evaluation of Read::readn: Invalid real number found when reading from StringToStream[123, xyz, 456].
(* $Failed *)
Word
the comma is left on the stream and throws the error. This "works"Read[str, {Number, Word, Character, Number}, WordSeparators -> {" ", ","}]
. Not satisfactory but it gives a clue whats happening. $\endgroup$ – george2079 Apr 11 '17 at 14:17Number
type does not utilize/respect the separator options. You can see that just like this :Read[StringToStream["1,2"], {Number, Number}, RecordSeparators -> {","}]
$\endgroup$ – george2079 Apr 11 '17 at 14:51