Loading ...
Sorry, an error occurred while loading the content.

Scanf: cannot see why "_" causes problems.

Expand Messages
  • Hugo Ferreira
    Hello, I have tried to use the Scanf module to parse a simple string as follows: # let file_id _ _ _ = true ;; val file_id : a - b - c - bool = #
    Message 1 of 4 , May 31, 2010
    View Source
    • 0 Attachment
      Hello,

      I have tried to use the Scanf module to parse a simple string
      as follows:

      # let file_id _ _ _ = true ;;
      val file_id : 'a -> 'b -> 'c -> bool = <fun>

      # Scanf.sscanf "0_8_72_tset" "%d_%d%s" file_id ;;
      Exception:
      Scanf.Scan_failure
      "scanf: bad input at char number 7: looking for '_', found 't'".


      I cannot understand why the "_" is problematic.
      If I substitute "_" for "?" I get:

      # Scanf.sscanf "0?8_72_tset" "%d?%d%s" file_id ;;
      - : bool = true


      Note that the only other use of "_" that I am aware of
      is for example "%_s" so that the element is ignored.

      Can anyone see what is the error that I am making?


      TIA,
      Hugo F.
    • Chantal Keller
      Hi, In the API of Scanf [1], one can read: in addition to the relevant digits, _ characters may appear inside numbers (this is reminiscent to the usual Caml
      Message 2 of 4 , May 31, 2010
      View Source
      • 0 Attachment
        Hi,

        In the API of Scanf [1], one can read:

        "in addition to the relevant digits, '_' characters may appear inside
        numbers (this is reminiscent to the usual Caml lexical conventions). If
        stricter scanning is desired, use the range conversion facility instead
        of the number conversions."

        So, the first %d corresponds to "0_8_72_", and it cannot find a "_"
        after.

        You just have to change to:

        Scanf.sscanf "0_8_72_tset" "%[0-9]_%[0-9]%s" file_id ;;

        Chantal.

        [1] http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.html




        > Hello,
        >
        > I have tried to use the Scanf module to parse a simple string
        > as follows:
        >
        > # let file_id _ _ _ = true ;;
        > val file_id : 'a -> 'b -> 'c -> bool = <fun>
        >
        > # Scanf.sscanf "0_8_72_tset" "%d_%d%s" file_id ;;
        > Exception:
        > Scanf.Scan_failure
        > "scanf: bad input at char number 7: looking for '_', found 't'".
        >
        >
        > I cannot understand why the "_" is problematic.
        > If I substitute "_" for "?" I get:
        >
        > # Scanf.sscanf "0?8_72_tset" "%d?%d%s" file_id ;;
        > - : bool = true
        >
        >
        > Note that the only other use of "_" that I am aware of
        > is for example "%_s" so that the element is ignored.
        >
        > Can anyone see what is the error that I am making?
        >
        >
        > TIA,
        > Hugo F.
      • Eric Cooper
        ... OCaml allows underscores in integer constants. They were introduced to make constants more readable (the way one would use , or . depending on
        Message 3 of 4 , May 31, 2010
        View Source
        • 0 Attachment
          On Mon, May 31, 2010 at 05:03:13PM +0100, Hugo Ferreira wrote:
          > I have tried to use the Scanf module to parse a simple string
          > as follows:
          >
          > # let file_id _ _ _ = true ;;
          > val file_id : 'a -> 'b -> 'c -> bool = <fun>
          >
          > # Scanf.sscanf "0_8_72_tset" "%d_%d%s" file_id ;;
          > Exception:
          > Scanf.Scan_failure
          > "scanf: bad input at char number 7: looking for '_', found 't'".
          >
          > I cannot understand why the "_" is problematic.

          OCaml allows underscores in integer constants. They were introduced
          to make constants more readable (the way one would use ',' or '.'
          depending on locale):

          # 1_000_000;;
          - : int = 1000000

          But there's no restriction on where they occur:

          # 1_0_00;;
          - : int = 1000
          # 1_0;;
          - : int = 10

          So sscanf parses the 0_8_72 as your first integer.

          --
          Eric Cooper e c c @ c m u . e d u
        • Hugo Ferreira
          Thanks Chantal Keller and Eric Cooper for the correction.
          Message 4 of 4 , May 31, 2010
          View Source
          • 0 Attachment
            Thanks Chantal Keller and Eric Cooper for the correction.

            Eric Cooper wrote:
            > On Mon, May 31, 2010 at 05:03:13PM +0100, Hugo Ferreira wrote:
            >> I have tried to use the Scanf module to parse a simple string
            >> as follows:
            >>
            >> # let file_id _ _ _ = true ;;
            >> val file_id : 'a -> 'b -> 'c -> bool = <fun>
            >>
            >> # Scanf.sscanf "0_8_72_tset" "%d_%d%s" file_id ;;
            >> Exception:
            >> Scanf.Scan_failure
            >> "scanf: bad input at char number 7: looking for '_', found 't'".
            >>
            >> I cannot understand why the "_" is problematic.
            >
            > OCaml allows underscores in integer constants. They were introduced
            > to make constants more readable (the way one would use ',' or '.'
            > depending on locale):
            >
            > # 1_000_000;;
            > - : int = 1000000
            >
            > But there's no restriction on where they occur:
            >
            > # 1_0_00;;
            > - : int = 1000
            > # 1_0;;
            > - : int = 10
            >
            > So sscanf parses the 0_8_72 as your first integer.
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.