* [Caml-list] Scanf.sscanf surprised me
@ 2013-10-08 2:58 Francois Berenger
2013-10-08 3:28 ` Robert Roessler
2013-10-08 15:23 ` Eric Cooper
0 siblings, 2 replies; 4+ messages in thread
From: Francois Berenger @ 2013-10-08 2:58 UTC (permalink / raw)
To: caml-list
This example line and scanning format work:
# let line = "active_ZINC01535869
0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379";;
val line : string =
"active_ZINC01535869
0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379"
# Scanf.sscanf line "%s %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" (fun name s1
s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11));;
- : string * float * float * float * float * float * float * float *
float * float * float * float
=
("active_ZINC01535869", 0.47, 0.389, 0.479, 0.453, 0.47, 0.631, 0.562,
0.59, 0.677, 0.558, 0.379)
This one doesn't:
# let line =
"active_ZINC01535869,0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379";;
val line :
string =
"active_ZINC01535869,0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379"
# Scanf.sscanf line "%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" (fun name s1
s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11));;
Exception: End_of_file.
I am quite surprised.
I was expecting the first separator being a space or a coma
to behave the same.
--
Best regards,
Francois Berenger.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Scanf.sscanf surprised me
2013-10-08 2:58 [Caml-list] Scanf.sscanf surprised me Francois Berenger
@ 2013-10-08 3:28 ` Robert Roessler
2013-10-08 4:30 ` Gabriel Scherer
2013-10-08 15:23 ` Eric Cooper
1 sibling, 1 reply; 4+ messages in thread
From: Robert Roessler @ 2013-10-08 3:28 UTC (permalink / raw)
To: Francois Berenger, caml-list
Francois Berenger wrote:
> This example line and scanning format work:
>
> # let line = "active_ZINC01535869
> 0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379";;
> val line : string =
> "active_ZINC01535869
> 0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379"
> # Scanf.sscanf line "%s %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" (fun name s1
> s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
> s8, s9, s10, s11));;
> - : string * float * float * float * float * float * float * float *
> float * float * float * float
> =
> ("active_ZINC01535869", 0.47, 0.389, 0.479, 0.453, 0.47, 0.631, 0.562,
> 0.59, 0.677, 0.558, 0.379)
>
> This one doesn't:
>
> # let line =
> "active_ZINC01535869,0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379";;
> val line :
> string =
>
> "active_ZINC01535869,0.470,0.389,0.479,0.453,0.470,0.631,0.562,0.590,0.677,0.558,0.379"
>
> # Scanf.sscanf line "%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" (fun name s1
> s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
> s8, s9, s10, s11));;
> Exception: End_of_file.
>
> I am quite surprised.
> I was expecting the first separator being a space or a coma
> to behave the same.
The specification of scanf "%s" is "Any number of non-whitespace
characters, stopping at the first whitespace character found".
So, perhaps not that surprising?
Regards,
--
Robert Roessler
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Scanf.sscanf surprised me
2013-10-08 3:28 ` Robert Roessler
@ 2013-10-08 4:30 ` Gabriel Scherer
0 siblings, 0 replies; 4+ messages in thread
From: Gabriel Scherer @ 2013-10-08 4:30 UTC (permalink / raw)
To: Robert Roessler; +Cc: Francois Berenger, caml users
[-- Attachment #1: Type: text/plain, Size: 2153 bytes --]
Use "%s@,%f..." to specify (with @) that the string should stop at the
first comma.
On Tue, Oct 8, 2013 at 5:28 AM, Robert Roessler <roessler@rftp.com> wrote:
> Francois Berenger wrote:
>
>> This example line and scanning format work:
>>
>> # let line = "active_ZINC01535869
>> 0.470,0.389,0.479,0.453,0.470,**0.631,0.562,0.590,0.677,0.558,**0.379";;
>> val line : string =
>> "active_ZINC01535869
>> 0.470,0.389,0.479,0.453,0.470,**0.631,0.562,0.590,0.677,0.558,**0.379"
>> # Scanf.sscanf line "%s %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,**%f" (fun name s1
>> s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
>> s8, s9, s10, s11));;
>> - : string * float * float * float * float * float * float * float *
>> float * float * float * float
>> =
>> ("active_ZINC01535869", 0.47, 0.389, 0.479, 0.453, 0.47, 0.631, 0.562,
>> 0.59, 0.677, 0.558, 0.379)
>>
>> This one doesn't:
>>
>> # let line =
>> "active_ZINC01535869,0.470,0.**389,0.479,0.453,0.470,0.631,0.**
>> 562,0.590,0.677,0.558,0.379";;
>> val line :
>> string =
>>
>> "active_ZINC01535869,0.470,0.**389,0.479,0.453,0.470,0.631,0.**
>> 562,0.590,0.677,0.558,0.379"
>>
>> # Scanf.sscanf line "%s,%f,%f,%f,%f,%f,%f,%f,%f,%**f,%f,%f" (fun name s1
>> s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 -> (name, s1, s2, s3, s4, s5, s6, s7,
>> s8, s9, s10, s11));;
>> Exception: End_of_file.
>>
>> I am quite surprised.
>> I was expecting the first separator being a space or a coma
>> to behave the same.
>>
>
> The specification of scanf "%s" is "Any number of non-whitespace
> characters, stopping at the first whitespace character found".
>
> So, perhaps not that surprising?
>
> Regards,
> --
> Robert Roessler
>
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa.inria.fr/sympa/**arc/caml-list<https://sympa.inria.fr/sympa/arc/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>
[-- Attachment #2: Type: text/html, Size: 3048 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Scanf.sscanf surprised me
2013-10-08 2:58 [Caml-list] Scanf.sscanf surprised me Francois Berenger
2013-10-08 3:28 ` Robert Roessler
@ 2013-10-08 15:23 ` Eric Cooper
1 sibling, 0 replies; 4+ messages in thread
From: Eric Cooper @ 2013-10-08 15:23 UTC (permalink / raw)
To: caml-list
To paraphrase Jamie Zawinski:
Some people, when confronted with a problem, think "I know, I'll use
scanf." Now they have two problems.
--
Eric Cooper e c c @ c m u . e d u
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-10-08 15:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-08 2:58 [Caml-list] Scanf.sscanf surprised me Francois Berenger
2013-10-08 3:28 ` Robert Roessler
2013-10-08 4:30 ` Gabriel Scherer
2013-10-08 15:23 ` Eric Cooper
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox