从字符串中读取整数
reading integers from a string
我想从文件中读取一行,从该行初始化一个数组,然后显示整数。
为什么不读取行中的五个整数?我想得到输出 1 2 3 4 5
,我有 1 1 1 1 1
open Array;;
open Scanf;;
let print_ints file_name =
let file = open_in file_name in
let s = input_line(file) in
let n = ref 5 in
let arr = Array.init !n (fun i -> if i < !n then sscanf s "%d" (fun a -> a) else 0) in
let i = ref 0 in
while !i < !n do
print_int (Array.get arr !i);
print_string " ";
i := !i + 1;
done;;
print_ints "string_ints.txt";;
我的文件是:1 2 3 4 5
您可能想尝试以下方法。将您的字符串拆分为代表数字的子字符串列表。 描述了一种方法。然后在 print_ints
函数中使用结果函数。
let ints_of_string s =
List.map int_of_string (Str.split (Str.regexp " +") s)
let print_ints file_name =
let file = open_in file_name in
let s = input_line file in
let ints = ints_of_string s in
List.iter (fun i -> print_int i; print_char ' ') ints;
close_in file
let _ = print_ints "string_ints.txt"
编译时,将str.cma
或str.cmxa
作为参数传递(有关编译的详细信息,请参阅this answer):
$ ocamlc str.cma print_ints.ml
另一种选择是使用 Scanf.bscanf
函数 -- ,包含一个示例(谨慎使用)。
Scanf.sscanf
函数可能不是特别适合这个任务。
摘自 OCaml manual:
the scanf facility is not intended for heavy duty lexical analysis and parsing. If it appears not expressive enough for your needs, several alternative exists: regular expressions (module Str), stream parsers, ocamllex-generated lexers, ocamlyacc-generated parsers
尽管有一种方法可以使用 Scanf.sscanf
解析整数字符串(我不推荐):
let rec int_list_of_string s =
try
Scanf.sscanf s
"%d %[0-9-+ ]"
(fun n rest_str -> n :: int_list_of_string rest_str)
with
| End_of_file | Scanf.Scan_failure _ -> []
这里的技巧是将输入字符串 s
表示为将被解析为整数 (%d
) 的一部分,并使用范围格式表示字符串的其余部分: %[0-9-+ ]"
,它将匹配字符串的其余部分,仅包含十进制数字 0-9
、-
和 +
符号以及空格
.
我想从文件中读取一行,从该行初始化一个数组,然后显示整数。
为什么不读取行中的五个整数?我想得到输出 1 2 3 4 5
,我有 1 1 1 1 1
open Array;;
open Scanf;;
let print_ints file_name =
let file = open_in file_name in
let s = input_line(file) in
let n = ref 5 in
let arr = Array.init !n (fun i -> if i < !n then sscanf s "%d" (fun a -> a) else 0) in
let i = ref 0 in
while !i < !n do
print_int (Array.get arr !i);
print_string " ";
i := !i + 1;
done;;
print_ints "string_ints.txt";;
我的文件是:1 2 3 4 5
您可能想尝试以下方法。将您的字符串拆分为代表数字的子字符串列表。 print_ints
函数中使用结果函数。
let ints_of_string s =
List.map int_of_string (Str.split (Str.regexp " +") s)
let print_ints file_name =
let file = open_in file_name in
let s = input_line file in
let ints = ints_of_string s in
List.iter (fun i -> print_int i; print_char ' ') ints;
close_in file
let _ = print_ints "string_ints.txt"
编译时,将str.cma
或str.cmxa
作为参数传递(有关编译的详细信息,请参阅this answer):
$ ocamlc str.cma print_ints.ml
另一种选择是使用 Scanf.bscanf
函数 --
Scanf.sscanf
函数可能不是特别适合这个任务。
摘自 OCaml manual:
the scanf facility is not intended for heavy duty lexical analysis and parsing. If it appears not expressive enough for your needs, several alternative exists: regular expressions (module Str), stream parsers, ocamllex-generated lexers, ocamlyacc-generated parsers
尽管有一种方法可以使用 Scanf.sscanf
解析整数字符串(我不推荐):
let rec int_list_of_string s =
try
Scanf.sscanf s
"%d %[0-9-+ ]"
(fun n rest_str -> n :: int_list_of_string rest_str)
with
| End_of_file | Scanf.Scan_failure _ -> []
这里的技巧是将输入字符串 s
表示为将被解析为整数 (%d
) 的一部分,并使用范围格式表示字符串的其余部分: %[0-9-+ ]"
,它将匹配字符串的其余部分,仅包含十进制数字 0-9
、-
和 +
符号以及空格 .