Reputation: 45
I want to read a line with this structure:
7 12 129224 2 124 12 2 51
And put the elements of the string in a int list like the following:
[7; 12; 129224; 2; 124; 12; 2; 51]
The numbers could have any size and negative numbers are not to be considered. Here is what I have so far:
let size_s = read_int();; (* size_s is the number of elements within the String. In this case it's 8. *)
let s = read_line();;
Upvotes: 3
Views: 1082
Reputation: 66793
I'm not sure about the initial 8 that you're reading. It doesn't show up in your example.
However, this old answer shows one way to convert a string to a list of ints: How to convert a string to integer list in ocaml?.
Here's an example for many sizes of int:
List.map int_of_string (Str.split (Str.regexp "[^0-9]+") "123 8 67 4");;
- : int list = [123; 8; 67; 4]
Upvotes: 1