1

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.

Legolas
  • 105
  • 8

2 Answers2

3

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
Louis M
  • 576
  • 2
  • 4
2

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
Eric Bezault
  • 141
  • 5