How would one read space separated input in Eiffel?
I can't really use Io.read_integer
as that just ignores everything except
the first number.
I can use Io.read_line
to get something like 15 14 59 86
.
How would I split these into integers now?
Javascript has split()
, C++ has stringstream
, something similar would be ideal.
input stream eiffel
add a comment |
I can't really use Io.read_integer
as that just ignores everything except
the first number.
I can use Io.read_line
to get something like 15 14 59 86
.
How would I split these into integers now?
Javascript has split()
, C++ has stringstream
, something similar would be ideal.
input stream eiffel
add a comment |
I can't really use Io.read_integer
as that just ignores everything except
the first number.
I can use Io.read_line
to get something like 15 14 59 86
.
How would I split these into integers now?
Javascript has split()
, C++ has stringstream
, something similar would be ideal.
input stream eiffel
I can't really use Io.read_integer
as that just ignores everything except
the first number.
I can use Io.read_line
to get something like 15 14 59 86
.
How would I split these into integers now?
Javascript has split()
, C++ has stringstream
, something similar would be ideal.
input stream eiffel
input stream eiffel
asked Nov 11 '18 at 10:01
Legolas
255
255
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you use only one space, there is a split
method in the {STRING}
class. The argument of the split
method is a {CHARACTER}
instead of a {STRING}
. So, you have to use ' '
instead of " "
. Here is a little function that do what I think you want.
split_to_integer_list(a_string:STRING):ARRAYED_LIST[INTEGER]
-- Convert `a_string', a space separated list of integer
-- into a {LIST} of {INTEGER}
local
l_split:LIST[STRING]
do
l_split := a_string.split (' ')
create Result.make (a_string.count)
across l_split as la_split loop
if la_split.item.is_integer then
Result.extend(la_split.item.to_integer)
end
end
end
add a comment |
In case you want to allow several spaces, tabs, etc. between the integers, you can use the class {ST_SPLITTER}
from the Gobo library. Here is an example:
local
l_line: STRING
l_splitter: ST_SPLITTER
l_list: DS_LINKED_LIST [INTEGER]
do
io.read_line
l_line := io.last_string
create l_list.make
create l_splitter.make_with_separators (" %T")
across l_splitter.split (l_line) as l_split loop
if l_split.item.is_integer then
l_list.put_last (l_split.item.to_integer)
end
end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247600%2fhow-would-one-read-space-separated-input-in-eiffel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you use only one space, there is a split
method in the {STRING}
class. The argument of the split
method is a {CHARACTER}
instead of a {STRING}
. So, you have to use ' '
instead of " "
. Here is a little function that do what I think you want.
split_to_integer_list(a_string:STRING):ARRAYED_LIST[INTEGER]
-- Convert `a_string', a space separated list of integer
-- into a {LIST} of {INTEGER}
local
l_split:LIST[STRING]
do
l_split := a_string.split (' ')
create Result.make (a_string.count)
across l_split as la_split loop
if la_split.item.is_integer then
Result.extend(la_split.item.to_integer)
end
end
end
add a comment |
If you use only one space, there is a split
method in the {STRING}
class. The argument of the split
method is a {CHARACTER}
instead of a {STRING}
. So, you have to use ' '
instead of " "
. Here is a little function that do what I think you want.
split_to_integer_list(a_string:STRING):ARRAYED_LIST[INTEGER]
-- Convert `a_string', a space separated list of integer
-- into a {LIST} of {INTEGER}
local
l_split:LIST[STRING]
do
l_split := a_string.split (' ')
create Result.make (a_string.count)
across l_split as la_split loop
if la_split.item.is_integer then
Result.extend(la_split.item.to_integer)
end
end
end
add a comment |
If you use only one space, there is a split
method in the {STRING}
class. The argument of the split
method is a {CHARACTER}
instead of a {STRING}
. So, you have to use ' '
instead of " "
. Here is a little function that do what I think you want.
split_to_integer_list(a_string:STRING):ARRAYED_LIST[INTEGER]
-- Convert `a_string', a space separated list of integer
-- into a {LIST} of {INTEGER}
local
l_split:LIST[STRING]
do
l_split := a_string.split (' ')
create Result.make (a_string.count)
across l_split as la_split loop
if la_split.item.is_integer then
Result.extend(la_split.item.to_integer)
end
end
end
If you use only one space, there is a split
method in the {STRING}
class. The argument of the split
method is a {CHARACTER}
instead of a {STRING}
. So, you have to use ' '
instead of " "
. Here is a little function that do what I think you want.
split_to_integer_list(a_string:STRING):ARRAYED_LIST[INTEGER]
-- Convert `a_string', a space separated list of integer
-- into a {LIST} of {INTEGER}
local
l_split:LIST[STRING]
do
l_split := a_string.split (' ')
create Result.make (a_string.count)
across l_split as la_split loop
if la_split.item.is_integer then
Result.extend(la_split.item.to_integer)
end
end
end
answered Nov 12 '18 at 0:55
Louis M
49124
49124
add a comment |
add a comment |
In case you want to allow several spaces, tabs, etc. between the integers, you can use the class {ST_SPLITTER}
from the Gobo library. Here is an example:
local
l_line: STRING
l_splitter: ST_SPLITTER
l_list: DS_LINKED_LIST [INTEGER]
do
io.read_line
l_line := io.last_string
create l_list.make
create l_splitter.make_with_separators (" %T")
across l_splitter.split (l_line) as l_split loop
if l_split.item.is_integer then
l_list.put_last (l_split.item.to_integer)
end
end
add a comment |
In case you want to allow several spaces, tabs, etc. between the integers, you can use the class {ST_SPLITTER}
from the Gobo library. Here is an example:
local
l_line: STRING
l_splitter: ST_SPLITTER
l_list: DS_LINKED_LIST [INTEGER]
do
io.read_line
l_line := io.last_string
create l_list.make
create l_splitter.make_with_separators (" %T")
across l_splitter.split (l_line) as l_split loop
if l_split.item.is_integer then
l_list.put_last (l_split.item.to_integer)
end
end
add a comment |
In case you want to allow several spaces, tabs, etc. between the integers, you can use the class {ST_SPLITTER}
from the Gobo library. Here is an example:
local
l_line: STRING
l_splitter: ST_SPLITTER
l_list: DS_LINKED_LIST [INTEGER]
do
io.read_line
l_line := io.last_string
create l_list.make
create l_splitter.make_with_separators (" %T")
across l_splitter.split (l_line) as l_split loop
if l_split.item.is_integer then
l_list.put_last (l_split.item.to_integer)
end
end
In case you want to allow several spaces, tabs, etc. between the integers, you can use the class {ST_SPLITTER}
from the Gobo library. Here is an example:
local
l_line: STRING
l_splitter: ST_SPLITTER
l_list: DS_LINKED_LIST [INTEGER]
do
io.read_line
l_line := io.last_string
create l_list.make
create l_splitter.make_with_separators (" %T")
across l_splitter.split (l_line) as l_split loop
if l_split.item.is_integer then
l_list.put_last (l_split.item.to_integer)
end
end
answered Nov 12 '18 at 8:46
Eric Bezault
414
414
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247600%2fhow-would-one-read-space-separated-input-in-eiffel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown