From: rich Date: Wed, 18 Oct 2006 14:56:12 +0000 (+0000) Subject: Csv.concat - arranges CSV subfiles in columns. X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=af6726a634255b49167e1aa90d92103c3769dc90;p=ocaml-csv.git Csv.concat - arranges CSV subfiles in columns. --- diff --git a/csv.ml b/csv.ml index 6ca424b..b76f4b3 100644 --- a/csv.ml +++ b/csv.ml @@ -1,6 +1,6 @@ (* csv.ml - comma separated values parser * - * $Id: csv.ml,v 1.11 2006-07-19 09:41:58 rich Exp $ + * $Id: csv.ml,v 1.12 2006-10-18 14:56:12 rich Exp $ *) (* The format of CSV files: @@ -299,6 +299,27 @@ let rec compare (csv1 : t) csv2 = let c = compare_row [] y in if c <> 0 then c else compare [] ys +(* Concatenate - arrange left to right. *) +let rec concat = function + | [] -> [] + | [csv] -> csv + | left_csv :: csvs -> + (* Concatenate the remaining CSV files. *) + let right_csv = concat csvs in + + (* Set the height of the left and right CSVs to the same. *) + let nr_rows = max (lines left_csv) (lines right_csv) in + let left_csv = set_rows nr_rows left_csv in + let right_csv = set_rows nr_rows right_csv in + + (* Square off the left CSV. *) + let left_csv = square left_csv in + + (* Prepend the right CSV rows with the left CSV rows. *) + List.map ( + fun (left_row, right_row) -> List.append left_row right_row + ) (List.combine left_csv right_csv) + let to_array csv = Array.of_list (List.map Array.of_list csv) diff --git a/csv.mli b/csv.mli index ef061e6..983fce1 100644 --- a/csv.mli +++ b/csv.mli @@ -1,6 +1,6 @@ (** csv.mli - comma separated values parser * - * $Id: csv.mli,v 1.8 2006-02-23 15:24:25 rich Exp $ + * $Id: csv.mli,v 1.9 2006-10-18 14:56:12 rich Exp $ *) type t = string list list @@ -112,6 +112,15 @@ val compare : t -> t -> int * look the same if opened in a spreadsheet program. *) +val concat : t list -> t +(** Concatenate CSV files so that they appear side by side, arranged + * left to right across the page. Each CSV file (except the final + * one) is first squared. + * + * (To concatenate CSV files so that they appear from top to bottom, + * just use {!List.concat}). + *) + val to_array : t -> string array array val of_array : string array array -> t (** Convenience functions to convert to and from a matrix representation.