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

Question about read_line()

Expand Messages
  • Sarah
    Hi all, Daft question this, but I couldn t figure it out quickly... if I take a string from the keyboard using read_line() how can I test it for special
    Message 1 of 4 , Apr 2, 2010
    • 0 Attachment
      Hi all,

      Daft question this, but I couldn't figure it out quickly...

      if I take a string from the keyboard using read_line() how can I test it for "special" characters, such as the keyboard "up" arrow, or Ctrl-r? I tried very naively testing against strings like "\027[A", but no dice.

      Many thanks,

      Sarah
    • Matthieu Dubuget
      ... let list_chars s = for i = 0 to pred (String.length s) do print_int (Char.code s.[i]); print_newline () done let s = ref let _ = while !s ciao do s
      Message 2 of 4 , Apr 2, 2010
      • 0 Attachment
        --------------essai.ml----------------
        let list_chars s =
        for i = 0 to pred (String.length s) do
        print_int (Char.code s.[i]);
        print_newline ()
        done

        let s = ref ""

        let _ =
        while !s <> "ciao" do
        s := read_line ();
        list_chars !s
        done
        ----------------------------------------------


        ocamlbuild essai.native
        ./essai.native
        ^[[A
        27
        91
        65
        ciao
        99
        105
        97
        111


        Does this help?

        Salutations

        Matt
      • Toby Kelsey
        ... If you just need command history/editing, try ledit http://www.ocaml-tutorial.org/ledit http://www.cocan.org/tips_for_using_the_ocaml_toplevel#Ledit
        Message 3 of 4 , Apr 2, 2010
        • 0 Attachment
          Sarah wrote:
          > Hi all,
          >
          > Daft question this, but I couldn't figure it out quickly...
          >
          > if I take a string from the keyboard using read_line() how can I test it for "special" characters, such as the keyboard "up" arrow, or Ctrl-r? I tried very naively testing against strings like "\027[A", but no dice.
          >
          > Many thanks,
          >
          > Sarah

          If you just need command history/editing, try ledit

          http://www.ocaml-tutorial.org/ledit

          http://www.cocan.org/tips_for_using_the_ocaml_toplevel#Ledit
        • vincent
          ... This works perfectly for me : # if read_line () = 027[A then true else false;; ^[[A - : bool = true # if read_line () = 027[A then true else false;;
          Message 4 of 4 , Apr 2, 2010
          • 0 Attachment
            --- In ocaml_beginners@yahoogroups.com, Toby Kelsey <toby.kelsey@...> wrote:
            >
            > Sarah wrote:
            > > Hi all,
            > >
            > > Daft question this, but I couldn't figure it out quickly...
            > >
            > > if I take a string from the keyboard using read_line() how can I test it for "special" characters, such as the keyboard "up" arrow, or Ctrl-r? I tried very naively testing against strings like "\027[A", but no dice.

            This works perfectly for me :

            # if read_line () = "\027[A" then true else false;;
            ^[[A
            - : bool = true
            # if read_line () = "\027[A" then true else false;;
            x
            - : bool = false

            > If you just need command history/editing, try ledit
            >
            > http://www.ocaml-tutorial.org/ledit
            >
            > http://www.cocan.org/tips_for_using_the_ocaml_toplevel#Ledit

            or rlwrap: http://freshmeat.net/projects/rlwrap/
          Your message has been successfully submitted and would be delivered to recipients shortly.