// // Legato Code Examples // -------------------- // // CSV Table Functions // // Group: Data // Title: Load and Save CSV as a Table // Description: A small program that demonstrates the use of CSVReadTable and CSVWriteTable functions by loading statistics from a file, makes a minor formatting change and then writes the data to a new file. // Include: CSV Example 01.csv // Revised: 12/21/2014 // // (c) Novaworks, LLC // string table[100][10]; // expand as needed string s1, s2; int rc, rx, cx, ix, cols, rows; int is_text; s1 = GetScriptFolder() + "CSV Example 01.csv"; // Load the Data table = CSVReadTable(s1); if (IsError()) { rc = GetLastError(); MessageBox('x', "Error reading file %08X", rc); exit; } cols = ArrayGetAxisDepth(table, AXIS_COL); rows = ArrayGetAxisDepth(table, AXIS_ROW); // Modify By Removing Parenthetical Data rx = 0; while (rx < rows) { cx = 0; while (cx < cols) { ix = InString(table[rx][cx], '('); if (ix >= 0) { table[rx][cx] = TrimString(GetStringSegment(table[rx][cx], 0, ix)); } cx++; } rx++; } // Write to the Log AddMessage("CSV -- Rows %d Cols %d", rows, cols); rx = 0; while (rx < rows) { s1 = ""; cx = 0; while (cx < cols) { s2 = GetStringSegment(table[rx][cx], 0, 20); s2 = PadString(s2, 20); s1 = s1 + s2; cx++; } AddMessage("%3d : %s", rx, s1); rx++; } // Write to File s1 = GetScriptFolder() + "CSV Output.csv"; rc = CSVWriteTable(table, s1); if (IsError(rc)) { MessageBox('x', "Error writing CSV table %08X", rc); exit; } MessageBox('i', "Completed, written to:\r\r%s", s1);