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

Re: grepping a large file (part 2)

Expand Messages
  • jshaw10
    It s simpler IMO to pass the accumulator as an argument. Furthermore you can clean things up a bit by separating the search part from the input/error handling
    Message 1 of 9 , Dec 30, 2009
      It's simpler IMO to pass the accumulator as an argument. Furthermore you can clean things up a bit by separating the search part from the input/error handling part.

      let find_str str =
      let rec get_line accumulator =
      match try Some (input_line mail_log) with End_of_file -> None with
      Some line -> search line accumulator
      | None -> accumulator
      and search line accumulator =
      get_line
      (
      if line_matched str line
      then line::accumulator
      else accumulator
      )
      in
      get_line []

      > You could always replace the while loop with a recursive function that
      > does the same thing, if you really wanted to..
      >
      > let find_str str =
      > let accumulator = ref [] in
      > try
      > let rec find_str_rec () =
      > let line = input_line mail_log in
      > if line_matched str line then accumulator := line :: !accumulator;
      > find_str_rec ()
      > in find_str_rec ()
      > with End_of_file -> !accumulator
      >
      > Lars Nilsson
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.