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

Index out of bounds using List.map

Expand Messages
  • sharifoerton
    Please forgive an eager newbie s constant pestering of the list while he learns the language. I have a large mess of code that is supposed to parse a text file
    Message 1 of 4 , Sep 2, 2008
    • 0 Attachment
      Please forgive an eager newbie's constant pestering of the list while
      he learns the language.

      I have a large mess of code that is supposed to parse a text file and
      return an array of arrays of integers.

      Code snippet:

      let ic = open_in filename in
      try
      while true do
      let line = input_line ic in
      reading_map := (if (Str.string_match map_pattern line 0) then
      true else !reading_map);
      reading_map := (if (Str.string_match map_end_pattern line 0)
      then false else !reading_map);
      if !reading_map then
      (
      let r = Str.split (Str.regexp "[ \t]+") line in
      let row = (Array.of_list (List.map int_of_string r)) in
      let temp = Array.to_list (!map) in
      let temp2 = row :: temp in
      map := (Array.of_list temp2);
      )
      done;

      It's crashing like so:

      Fatal error: exception Invalid_argument("index out of bounds")

      and I've tracked down the error to the line:

      let row = (Array.of_list (List.map int_of_string r)) in

      I don't understand this at all. How can List.map raise an index out of
      bounds error?

      Thanks in advance.

      Sharif
    • Fabrice Marchant
      On Tue, 02 Sep 2008 12:57:38 -0000 sharifoerton wrote: Hi ! ... Indeed, I do not see on this line any function able to generate such
      Message 2 of 4 , Sep 2, 2008
      • 0 Attachment
        On Tue, 02 Sep 2008 12:57:38 -0000
        "sharifoerton" <sharifoerton@...> wrote:

        Hi !

        > Fatal error: exception Invalid_argument("index out of bounds")
        >
        > and I've tracked down the error to the line:
        >
        > let row = (Array.of_list (List.map int_of_string r)) in
        >
        > I don't understand this at all.

        Indeed, I do not see on this line any function able to generate such an error.

        It would be kind of you if you complete your code, defining reading_map, map_patterns ... and providing a bugging example of filename.

        So we could reproduce the error and the problem would maybe be easier to fix.

        Thanks,

        Regards,

        Fabrice
      • Florent Monnier
        ... Maybe the way you tracked down the error is wrong, you could try this method, to print a stack backtrace in case of an uncaught exception: export
        Message 3 of 4 , Sep 2, 2008
        • 0 Attachment
          > Fatal error: exception Invalid_argument("index out of bounds")
          >
          > and I've tracked down the error to the line:
          >
          > let row = (Array.of_list (List.map int_of_string r)) in
          >
          > I don't understand this at all. How can List.map raise an index out of
          > bounds error?

          Maybe the way you tracked down the error is wrong, you could try this method,
          to print a stack backtrace in case of an uncaught exception:

          export OCAMLRUNPARAM='b'
          ocamlc -g test.ml -o test.byte
          ocamlrun test.byte

          it only works compiled in bytecode, and you must add the "-g" parameter to
          ocamlc (for several files, all objects have to be compiled with -g)

          --
        • Sharif Oerton
          Thank you very much - I had indeed failed to locate the error correctly - I guess printf debugging doesn t really suffice. It ended up being a simple logic
          Message 4 of 4 , Sep 3, 2008
          • 0 Attachment
            Thank you very much - I had indeed failed to locate the error correctly - I guess "printf debugging" doesn't really suffice. It ended up being a simple logic error.

            So sorry for wasting your time!

            Sharif
          Your message has been successfully submitted and would be delivered to recipients shortly.