Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -3,8 +3,8 @@ PROC char count = (CHAR c, STRING str) INT:
BEGIN
INT count := 0;
FOR i TO UPB str DO
IF c = str[i] THEN count +:= 1
FI
IF c = str[i] THEN count +:= 1
FI
OD;
count
END;
@ -17,9 +17,9 @@ PROC char split = (STRING str, CHAR sep) FLEX[]STRING :
INT start := 1;
[char count (sep, str) + 1] STRING list;
WHILE start <= strlen ANDF char in string (sep, p, str[start:]) DO
p +:= start - 1;
list[cnt +:= 1] := str[start:p-1];
start := p + 1
p +:= start - 1;
list[cnt +:= 1] := str[start:p-1];
start := p + 1
OD;
IF cnt = 0 THEN list[cnt +:= 1] := str
ELIF start <= UPB str + 1 THEN list[cnt +:= 1] := str[start:]
@ -31,7 +31,7 @@ PROC join = ([]STRING words, STRING sep) STRING:
IF UPB words > 0 THEN
STRING str := words [1];
FOR i FROM 2 TO UPB words DO
str +:= sep + words[i]
str +:= sep + words[i]
OD;
str
ELSE
@ -60,10 +60,10 @@ OP TOINT = (STRING str) INT:
BEGIN
INT n := 0, sign := 1;
FOR i TO UPB str WHILE sign /= 0 DO
IF is digit (str[i]) THEN n := n * 10 + ABS str[i] - ABS "0"
ELIF i = 1 AND str[i] = "-" THEN sign := -1
ELIF i /= 1 OR str[i] /= "+" THEN sign := 0
FI
IF is digit (str[i]) THEN n := n * 10 + ABS str[i] - ABS "0"
ELIF i = 1 AND str[i] = "-" THEN sign := -1
ELIF i /= 1 OR str[i] /= "+" THEN sign := 0
FI
OD;
n * sign
END;

View file

@ -6,7 +6,7 @@ NR==1 {
}
{
sum = 0
for (i=1; i<=NF; i++) {
for (i=1; i<=NF; i++) {
sum += $i
}
print $0, sum

View file

@ -1,19 +0,0 @@
package CSV is
type Row(<>) is tagged private;
function Line(S: String; Separator: Character := ',') return Row;
function Next(R: in out Row) return Boolean;
-- if there is still an item in R, Next advances to it and returns True
function Item(R: Row) return String;
-- after calling R.Next i times, this returns the i'th item (if any)
private
type Row(Length: Natural) is tagged record
Str: String(1 .. Length);
Fst: Positive;
Lst: Natural;
Nxt: Positive;
Sep: Character;
end record;
end CSV;

View file

@ -1,24 +0,0 @@
package body CSV is
function Line(S: String; Separator: Character := ',')
return Row is
(Length => S'Length, Str => S,
Fst => S'First, Lst => S'Last, Nxt => S'First, Sep => Separator);
function Item(R: Row) return String is
(R.Str(R.Fst .. R.Lst));
function Next(R: in out Row) return Boolean is
Last: Natural := R.Nxt;
begin
R.Fst := R.Nxt;
while Last <= R.Str'Last and then R.Str(Last) /= R.Sep loop
-- find Separator
Last := Last + 1;
end loop;
R.Lst := Last - 1;
R.Nxt := Last + 1;
return (R.Fst <= R.Str'Last);
end Next;
end CSV;

View file

@ -1,19 +0,0 @@
with CSV, Ada.Text_IO; use Ada.Text_IO;
procedure CSV_Data_Manipulation is
Header: String := Get_Line;
begin
Put_Line(Header & ", SUM");
while not End_Of_File loop
declare
R: CSV.Row := CSV.Line(Get_Line);
Sum: Integer := 0;
begin
while R.Next loop
Sum := Sum + Integer'Value(R.Item);
Put(R.Item & ",");
end loop;
Put_Line(Integer'Image(Sum));
end;
end loop;
end CSV_Data_Manipulation;

View file

@ -16,10 +16,10 @@
typedef struct {
char * delim;
unsigned int rows;
unsigned int cols;
char ** table;
char * delim;
unsigned int rows;
unsigned int cols;
char ** table;
} CSV;
@ -27,28 +27,28 @@ typedef struct {
* Utility function to trim whitespaces from left & right of a string
*/
int trim(char ** str) {
int trimmed;
int n;
int len;
len = strlen(*str);
n = len - 1;
/* from right */
while((n>=0) && isspace((*str)[n])) {
(*str)[n] = '\0';
trimmed += 1;
n--;
}
int trimmed;
int n;
int len;
/* from left */
n = 0;
while((n < len) && (isspace((*str)[0]))) {
(*str)[0] = '\0';
*str = (*str)+1;
trimmed += 1;
n++;
}
return trimmed;
len = strlen(*str);
n = len - 1;
/* from right */
while((n>=0) && isspace((*str)[n])) {
(*str)[n] = '\0';
trimmed += 1;
n--;
}
/* from left */
n = 0;
while((n < len) && (isspace((*str)[0]))) {
(*str)[0] = '\0';
*str = (*str)+1;
trimmed += 1;
n++;
}
return trimmed;
}
@ -56,11 +56,11 @@ int trim(char ** str) {
* De-allocate csv structure
*/
int csv_destroy(CSV * csv) {
if (csv == NULL) { return 0; }
if (csv->table != NULL) { free(csv->table); }
if (csv->delim != NULL) { free(csv->delim); }
free(csv);
return 0;
if (csv == NULL) { return 0; }
if (csv->table != NULL) { free(csv->table); }
if (csv->delim != NULL) { free(csv->delim); }
free(csv);
return 0;
}
@ -68,23 +68,23 @@ int csv_destroy(CSV * csv) {
* Allocate memory for a CSV structure
*/
CSV * csv_create(unsigned int cols, unsigned int rows) {
CSV * csv;
csv = malloc(sizeof(CSV));
csv->rows = rows;
csv->cols = cols;
csv->delim = strdup(",");
CSV * csv;
csv->table = malloc(sizeof(char *) * cols * rows);
if (csv->table == NULL) { goto error; }
csv = malloc(sizeof(CSV));
csv->rows = rows;
csv->cols = cols;
csv->delim = strdup(",");
memset(csv->table, 0, sizeof(char *) * cols * rows);
csv->table = malloc(sizeof(char *) * cols * rows);
if (csv->table == NULL) { goto error; }
return csv;
memset(csv->table, 0, sizeof(char *) * cols * rows);
return csv;
error:
csv_destroy(csv);
return NULL;
csv_destroy(csv);
return NULL;
}
@ -92,9 +92,9 @@ error:
* Get value in CSV table at COL, ROW
*/
char * csv_get(CSV * csv, unsigned int col, unsigned int row) {
unsigned int idx;
idx = col + (row * csv->cols);
return csv->table[idx];
unsigned int idx;
idx = col + (row * csv->cols);
return csv->table[idx];
}
@ -102,84 +102,84 @@ char * csv_get(CSV * csv, unsigned int col, unsigned int row) {
* Set value in CSV table at COL, ROW
*/
int csv_set(CSV * csv, unsigned int col, unsigned int row, char * value) {
unsigned int idx;
idx = col + (row * csv->cols);
csv->table[idx] = value;
return 0;
unsigned int idx;
idx = col + (row * csv->cols);
csv->table[idx] = value;
return 0;
}
void csv_display(CSV * csv) {
int row, col;
char * content;
if ((csv->rows == 0) || (csv->cols==0)) {
printf("[Empty table]\n");
return ;
}
int row, col;
char * content;
if ((csv->rows == 0) || (csv->cols==0)) {
printf("[Empty table]\n");
return ;
}
printf("\n[Table cols=%d rows=%d]\n", csv->cols, csv->rows);
for (row=0; row<csv->rows; row++) {
printf("[|");
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
printf("\n[Table cols=%d rows=%d]\n", csv->cols, csv->rows);
for (row=0; row<csv->rows; row++) {
printf("[|");
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
printf("%s\t|", content);
}
}
printf("]\n");
}
printf("\n");
}
printf("\n");
}
/**
* Resize CSV table
*/
int csv_resize(CSV * old_csv, unsigned int new_cols, unsigned int new_rows) {
unsigned int cur_col,
cur_row,
max_cols,
max_rows;
CSV * new_csv;
char * content;
bool in_old, in_new;
unsigned int cur_col,
cur_row,
max_cols,
max_rows;
CSV * new_csv;
char * content;
bool in_old, in_new;
/* Build a new (fake) csv */
new_csv = csv_create(new_cols, new_rows);
if (new_csv == NULL) { goto error; }
/* Build a new (fake) csv */
new_csv = csv_create(new_cols, new_rows);
if (new_csv == NULL) { goto error; }
new_csv->rows = new_rows;
new_csv->cols = new_cols;
new_csv->rows = new_rows;
new_csv->cols = new_cols;
max_cols = (new_cols > old_csv->cols)? new_cols : old_csv->cols;
max_rows = (new_rows > old_csv->rows)? new_rows : old_csv->rows;
max_cols = (new_cols > old_csv->cols)? new_cols : old_csv->cols;
max_rows = (new_rows > old_csv->rows)? new_rows : old_csv->rows;
for (cur_col=0; cur_col<max_cols; cur_col++) {
for (cur_row=0; cur_row<max_rows; cur_row++) {
in_old = (cur_col < old_csv->cols) && (cur_row < old_csv->rows);
in_new = (cur_col < new_csv->cols) && (cur_row < new_csv->rows);
for (cur_col=0; cur_col<max_cols; cur_col++) {
for (cur_row=0; cur_row<max_rows; cur_row++) {
in_old = (cur_col < old_csv->cols) && (cur_row < old_csv->rows);
in_new = (cur_col < new_csv->cols) && (cur_row < new_csv->rows);
if (in_old && in_new) {
/* re-link data */
content = csv_get(old_csv, cur_col, cur_row);
csv_set(new_csv, cur_col, cur_row, content);
} else if (in_old) {
/* destroy data */
content = csv_get(old_csv, cur_col, cur_row);
free(content);
} else { /* skip */ }
}
}
/* on rows */
free(old_csv->table);
old_csv->rows = new_rows;
old_csv->cols = new_cols;
old_csv->table = new_csv->table;
new_csv->table = NULL;
csv_destroy(new_csv);
if (in_old && in_new) {
/* re-link data */
content = csv_get(old_csv, cur_col, cur_row);
csv_set(new_csv, cur_col, cur_row, content);
} else if (in_old) {
/* destroy data */
content = csv_get(old_csv, cur_col, cur_row);
free(content);
} else { /* skip */ }
}
}
/* on rows */
free(old_csv->table);
old_csv->rows = new_rows;
old_csv->cols = new_cols;
old_csv->table = new_csv->table;
new_csv->table = NULL;
csv_destroy(new_csv);
return 0;
return 0;
error:
printf("Unable to resize CSV table: error %d - %s\n", errno, strerror(errno));
return -1;
printf("Unable to resize CSV table: error %d - %s\n", errno, strerror(errno));
return -1;
}
@ -187,42 +187,42 @@ error:
* Open CSV file and load its content into provided CSV structure
**/
int csv_open(CSV * csv, char * filename) {
FILE * fp;
unsigned int m_rows;
unsigned int m_cols, cols;
char line[2048];
char * lineptr;
char * token;
FILE * fp;
unsigned int m_rows;
unsigned int m_cols, cols;
char line[2048];
char * lineptr;
char * token;
fp = fopen(filename, "r");
if (fp == NULL) { goto error; }
fp = fopen(filename, "r");
if (fp == NULL) { goto error; }
m_rows = 0;
m_cols = 0;
while(fgets(line, sizeof(line), fp) != NULL) {
m_rows += 1;
cols = 0;
lineptr = line;
while ((token = strtok(lineptr, csv->delim)) != NULL) {
lineptr = NULL;
trim(&token);
m_rows = 0;
m_cols = 0;
while(fgets(line, sizeof(line), fp) != NULL) {
m_rows += 1;
cols = 0;
lineptr = line;
while ((token = strtok(lineptr, csv->delim)) != NULL) {
lineptr = NULL;
trim(&token);
cols += 1;
if (cols > m_cols) { m_cols = cols; }
if (cols > m_cols) { m_cols = cols; }
csv_resize(csv, m_cols, m_rows);
csv_set(csv, cols-1, m_rows-1, strdup(token));
}
}
}
fclose(fp);
csv->rows = m_rows;
csv->cols = m_cols;
return 0;
fclose(fp);
csv->rows = m_rows;
csv->cols = m_cols;
return 0;
error:
fclose(fp);
printf("Unable to open %s for reading.", filename);
return -1;
fclose(fp);
printf("Unable to open %s for reading.", filename);
return -1;
}
@ -230,22 +230,22 @@ error:
* Open CSV file and save CSV structure content into it
**/
int csv_save(CSV * csv, char * filename) {
FILE * fp;
int row, col;
char * content;
FILE * fp;
int row, col;
char * content;
fp = fopen(filename, "w");
for (row=0; row<csv->rows; row++) {
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
fp = fopen(filename, "w");
for (row=0; row<csv->rows; row++) {
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
fprintf(fp, "%s%s", content,
((col == csv->cols-1) ? "" : csv->delim) );
}
((col == csv->cols-1) ? "" : csv->delim) );
}
fprintf(fp, "\n");
}
}
fclose(fp);
return 0;
fclose(fp);
return 0;
}
@ -253,23 +253,23 @@ int csv_save(CSV * csv, char * filename) {
* Test
*/
int main(int argc, char ** argv) {
CSV * csv;
CSV * csv;
printf("%s\n%s\n\n",TITLE, URL);
printf("%s\n%s\n\n",TITLE, URL);
csv = csv_create(0, 0);
csv_open(csv, "fixtures/csv-data-manipulation.csv");
csv_display(csv);
csv = csv_create(0, 0);
csv_open(csv, "fixtures/csv-data-manipulation.csv");
csv_display(csv);
csv_set(csv, 0, 0, "Column0");
csv_set(csv, 1, 1, "100");
csv_set(csv, 2, 2, "200");
csv_set(csv, 3, 3, "300");
csv_set(csv, 4, 4, "400");
csv_display(csv);
csv_set(csv, 0, 0, "Column0");
csv_set(csv, 1, 1, "100");
csv_set(csv, 2, 2, "200");
csv_set(csv, 3, 3, "300");
csv_set(csv, 4, 4, "400");
csv_display(csv);
csv_save(csv, "tmp/csv-data-manipulation.result.csv");
csv_destroy(csv);
csv_save(csv, "tmp/csv-data-manipulation.result.csv");
csv_destroy(csv);
return 0;
return 0;
}

View file

@ -1,93 +0,0 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. CSV.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 19 December 2021.
************************************************************
** Program Abstract:
** CSVs are something COBOL does pretty well.
** The commented out CONCATENATE statements are a
** second method other than the STRING method.
************************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CSV-File ASSIGN TO "csv.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT Out-File ASSIGN TO "new.csv.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CSV-File
DATA RECORD IS CSV-Record.
01 CSV-Record.
05 Field1 PIC X(64).
FD Out-File
DATA RECORD IS Out-Line.
01 Out-Line PIC X(80).
WORKING-STORAGE SECTION.
01 Eof PIC X VALUE 'F'.
01 CSV-Data.
05 CSV-Col1 PIC 9(5).
05 CSV-Col2 PIC 9(5).
05 CSV-Col3 PIC 9(5).
05 CSV-Col4 PIC 9(5).
05 CSV-Col5 PIC 9(5).
01 CSV-Sum PIC ZZZ9.
01 CSV-Sum-Alpha
REDEFINES CSV-Sum PIC X(4).
PROCEDURE DIVISION.
Main-Program.
OPEN INPUT CSV-File
OPEN OUTPUT Out-File
PERFORM Read-a-Record
PERFORM Build-Header
PERFORM UNTIL Eof = 'T'
PERFORM Read-a-Record
IF Eof NOT EQUAL 'T' PERFORM Process-a-Record
END-PERFORM
CLOSE CSV-File
CLOSE Out-File
STOP RUN.
Read-a-Record.
READ CSV-File
AT END MOVE 'T' TO Eof
END-READ.
Build-Header.
** MOVE CONCATENATE(TRIM(CSV-Record), ",SUM"
** TO Out-Line.
STRING TRIM(CSV-Record), ",SUM" INTO Out-Line.
WRITE Out-Line.
MOVE SPACES TO Out-Line.
Process-a-Record.
UNSTRING CSV-Record DELIMITED BY ',' INTO
CSV-Col1 CSV-Col2 CSV-Col3 CSV-Col4 CSV-Col5.
COMPUTE CSV-Sum =
CSV-Col1 + CSV-Col2 + CSV-Col3 + CSV-Col4 + CSV-Col5.
** MOVE CONCATENATE(TRIM(CSV-Record), "," TRIM(CSV-Sum-Alpha))
** TO Out-Line.
STRING TRIM(CSV-Record), "," TRIM(CSV-Sum-Alpha)
INTO Out-Line.
WRITE Out-Line.
MOVE SPACES TO Out-Line.
END-PROGRAM.

View file

@ -5,7 +5,7 @@ STRING Field2;
STRING Field3;
STRING Field4;
STRING Field5;
END;
END;
MyDataset := DATASET ('~Rosetta::myCSVFile', MyFileLayout,CSV(SEPARATOR(',')));

View file

@ -10,12 +10,10 @@
(define (task file)
(let*
((table (csv->table file))
(header (first table))
(rows (rest table)))
(table->csv
(append header "SUM") ;; add last column
(for/list ((row rows)) (append row (apply + row))))))
((table (csv->table file))
(header (first table))
(rows (rest table)))
(table->csv
(append header "SUM") ;; add last column
(for/list ((row rows)) (append row (apply + row))))))

View file

@ -1,91 +0,0 @@
--- Read CSV file and add columns headed with 'SUM'
--- with trace
-- trace(0)
include get.e
include std/text.e
function split(sequence s, integer c)
sequence removables = " \t\n\r\x05\u0234\" "
sequence out
integer first, delim
out = {}
first = 1
while first <= length(s) do
delim = find_from(c,s,first)
if delim = 0 then
delim = length(s)+1
end if
out = append(out,trim(s[first..delim-1],removables))
first = delim + 1
end while
return out
end function
procedure main()
integer fn -- the file number
integer fn2 -- the output file number
integer e -- the number of lines read
object line -- the next line from the file
sequence data = {} -- parsed csv data row
sequence headerNames = {} -- array saving column names
atom sum = 0.0 -- sum for each row
sequence var -- holds numerical data read
-- First we try to open the file called "data.csv".
fn = open("data.csv", "r")
if fn = -1 then
puts(1, "Can't open data.csv\n")
-- abort();
end if
-- Then we create an output file for processed data.
fn2 = open("newdata.csv", "w")
if fn2 = -1 then
puts(1, "Can't create newdata.csv\n")
end if
-- By successfully opening the file we have established that
-- the file exists, and open() gives us a file number (or "handle")
-- that we can use to perform operations on the file.
e = 1
while 1 do
line = gets(fn)
if atom(line) then
exit
end if
data = split(line, ',')
if (e=1) then
-- Save the header labels and
-- write them to output file.
headerNames = data
for i=1 to length(headerNames) do
printf(fn2, "%s,", {headerNames[i]})
end for
printf(fn2, "SUM\n")
end if
-- Run a sum for the numerical data.
if (e >= 2) then
for i=1 to length(data) do
printf(fn2, "%s,", {data[i]})
var = value(data[i])
if var[1] = 0 then
-- data read is numerical
-- add to sum
sum = sum + var[2]
end if
end for
printf(fn2, "%g\n", {sum})
sum = 0.0
end if
e = e + 1
end while
close(fn)
close(fn2)
end procedure
main()

View file

@ -1,20 +1,20 @@
Copies a file with 5 comma-separated values to a line, appending a column holding their sum.
INTEGER N !Instead of littering the source with "5"
PARAMETER (N = 5) !Provide some provenance.
CHARACTER*6 HEAD(N) !A perfect size?
INTEGER X(N) !Integers suffice.
INTEGER LINPR,IN !I/O unit numbers.
LINPR = 6 !Standard output via this unit number.
IN = 10 !Some unit number for the input file.
OPEN (IN,FILE="CSVtest.csv",STATUS="OLD",ACTION="READ") !For formatted input.
INTEGER N !Instead of littering the source with "5"
PARAMETER (N = 5) !Provide some provenance.
CHARACTER*6 HEAD(N) !A perfect size?
INTEGER X(N) !Integers suffice.
INTEGER LINPR,IN !I/O unit numbers.
LINPR = 6 !Standard output via this unit number.
IN = 10 !Some unit number for the input file.
OPEN (IN,FILE="CSVtest.csv",STATUS="OLD",ACTION="READ") !For formatted input.
READ (IN,*) HEAD !The first line has texts as column headings.
WRITE (LINPR,1) (TRIM(HEAD(I)), I = 1,N),"Sum" !Append a "Sum" column.
1 FORMAT (666(A:",")) !The : sez "stop if no list element awaits".
2 READ (IN,*,END = 10) X !Read a line's worth of numbers, separated by commas or spaces.
WRITE (LINPR,3) X,SUM(X) !Write, with a total appended.
3 FORMAT (666(I0:",")) !I0 editing uses only as many columns as are needed.
GO TO 2 !Do it again.
READ (IN,*) HEAD !The first line has texts as column headings.
WRITE (LINPR,1) (TRIM(HEAD(I)), I = 1,N),"Sum" !Append a "Sum" column.
1 FORMAT (666(A:",")) !The : sez "stop if no list element awaits".
2 READ (IN,*,END = 10) X !Read a line's worth of numbers, separated by commas or spaces.
WRITE (LINPR,3) X,SUM(X) !Write, with a total appended.
3 FORMAT (666(I0:",")) !I0 editing uses only as many columns as are needed.
GO TO 2 !Do it again.
10 CLOSE (IN) !All done.
END !That's all.
10 CLOSE (IN) !All done.
END !That's all.

View file

@ -34,7 +34,7 @@ def updateRow( header, row, updates ) =
updates( r )
vector( r(f) | f <- header )
def update( table, updates ) =
def update( table, updates ) =
Table( table.header, (updateRow(table.header, r, updates) | r <- table.rows).toList() )
def addColumn( table, column, updates ) =

View file

@ -1,58 +1,58 @@
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
"encoding/csv"
"log"
"os"
"strconv"
)
func main() {
rows := readSample()
appendSum(rows)
writeChanges(rows)
rows := readSample()
appendSum(rows)
writeChanges(rows)
}
func readSample() [][]string {
f, err := os.Open("sample.csv")
if err != nil {
log.Fatal(err)
}
rows, err := csv.NewReader(f).ReadAll()
f.Close()
if err != nil {
log.Fatal(err)
}
return rows
f, err := os.Open("sample.csv")
if err != nil {
log.Fatal(err)
}
rows, err := csv.NewReader(f).ReadAll()
f.Close()
if err != nil {
log.Fatal(err)
}
return rows
}
func appendSum(rows [][]string) {
rows[0] = append(rows[0], "SUM")
for i := 1; i < len(rows); i++ {
rows[i] = append(rows[i], sum(rows[i]))
}
rows[0] = append(rows[0], "SUM")
for i := 1; i < len(rows); i++ {
rows[i] = append(rows[i], sum(rows[i]))
}
}
func sum(row []string) string {
sum := 0
for _, s := range row {
x, err := strconv.Atoi(s)
if err != nil {
return "NA"
}
sum += x
}
return strconv.Itoa(sum)
sum := 0
for _, s := range row {
x, err := strconv.Atoi(s)
if err != nil {
return "NA"
}
sum += x
}
return strconv.Itoa(sum)
}
func writeChanges(rows [][]string) {
f, err := os.Create("output.csv")
if err != nil {
log.Fatal(err)
}
err = csv.NewWriter(f).WriteAll(rows)
f.Close()
if err != nil {
log.Fatal(err)
}
f, err := os.Create("output.csv")
if err != nil {
log.Fatal(err)
}
err = csv.NewWriter(f).WriteAll(rows)
f.Close()
if err != nil {
log.Fatal(err)
}
}

View file

@ -1,19 +1,19 @@
to csv.data.manipulation :in :out
local [header line list sum]
openread :in
setread :in
openwrite :out
setwrite :out
make "header readword
openread :in
setread :in
openwrite :out
setwrite :out
make "header readword
print word :header ",SUM
while [not eofp] [
make "line readword
make "list parse map [ifelse equalp ? ", ["\ ] [?]] :line
make "sum apply "sum :list
print (word :line "\, :sum)
]
close :in
setread []
close :out
setwrite []
while [not eofp] [
make "line readword
make "list parse map [ifelse equalp ? ", ["\ ] [?]] :line
make "sum apply "sum :list
print (word :line "\, :sum)
]
close :in
setread []
close :out
setwrite []
end

View file

@ -3,11 +3,9 @@
print(io.read"l" .. ",SUM")
for line in io.lines() do
local fields, sum = {}, 0
for field in line:gmatch"[^,]+" do
table.insert(fields, field)
sum = sum + field
end
table.insert(fields, sum)
print(table.concat(fields,","))
local sum = 0
for field in line:gmatch"[^,]+" do
sum = sum + field
end
print(line .. "," .. sum)
end

View file

@ -7,7 +7,7 @@ X = dlmread(filename,',',1,0);
fid = fopen('data.out.csv','w+');
fprintf(fid,'%s,sum\n',header);
for k=1:size(X,1),
fprintf(fid,"%i,",X(k,:));
fprintf(fid,"%i\n",sum(X(k,:)));
fprintf(fid,"%i,",X(k,:));
fprintf(fid,"%i\n",sum(X(k,:)));
end;
fclose(fid);

View file

@ -1,33 +0,0 @@
## Create a CSV file
@"
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
"@ -split "`r`n" | Out-File -FilePath .\Temp.csv -Force
## Import each line of the CSV file into an array of PowerShell objects
$records = Import-Csv -Path .\Temp.csv
## Sum the values of the properties of each object
$sums = $records | ForEach-Object {
[int]$sum = 0
foreach ($field in $_.PSObject.Properties.Name)
{
$sum += $_.$field
}
$sum
}
## Add a column (Sum) and its value to each object in the array
$records = for ($i = 0; $i -lt $sums.Count; $i++)
{
$records[$i] | Select-Object *,@{Name='Sum';Expression={$sums[$i]}}
}
## Export the array of modified objects to the CSV file
$records | Export-Csv -Path .\Temp.csv -Force
## Display the object in tabular form
$records | Format-Table -AutoSize

View file

@ -2,21 +2,21 @@ test :- augment('test.csv', 'test.out.csv').
% augment( +InFileName, +OutFileName)
augment(InFile, OutFile) :-
open(OutFile, write, OutStream),
( ( csv_read_file_row(InFile, Row, [line(Line)]),
% Row is of the form row( Item1, Item2, ....).
addrow(Row, Out),
csv_write_stream(OutStream, [Out], []),
fail
)
; close(OutStream)
).
open(OutFile, write, OutStream),
( ( csv_read_file_row(InFile, Row, [line(Line)]),
% Row is of the form row( Item1, Item2, ....).
addrow(Row, Out),
csv_write_stream(OutStream, [Out], []),
fail
)
; close(OutStream)
).
% If the first item in a row is an integer, then append the sum;
% otherwise append 'SUM':
addrow( Term, NewTerm ) :-
Term =.. [F | List],
List = [X|_],
(integer(X) -> sum_list(List, Sum) ; Sum = 'SUM'),
append(List, [Sum], NewList),
NewTerm =.. [F | NewList].
Term =.. [F | List],
List = [X|_],
(integer(X) -> sum_list(List, Sum) ; Sum = 'SUM'),
append(List, [Sum], NewList),
NewTerm =.. [F | NewList].

View file

@ -1,6 +1,6 @@
>>forall data [either (index? data) = 1[
append data/1 "SUM"
append data/1 "SUM"
][
append data/1 to string!
(to integer! data/1/1) + (to integer! data/1/2) + (to integer! data/1/3) + (to integer! data/1/4) + (to integer! data/1/5)
append data/1 to string!
(to integer! data/1/1) + (to integer! data/1/2) + (to integer! data/1/3) + (to integer! data/1/4) + (to integer! data/1/5)
]]

View file

@ -3,10 +3,10 @@ import scala.io.Source
object parseCSV extends App {
val rawData = """|C1,C2,C3,C4,C5
|1,5,9,13,17
|2,6,10,14,18
|3,7,11,15,19
|20,21,22,23,24""".stripMargin
|1,5,9,13,17
|2,6,10,14,18
|3,7,11,15,19
|20,21,22,23,24""".stripMargin
val data = Seq((Source.fromString(rawData).getLines()).map(_.split(",")).toSeq: _*)

View file

@ -14,7 +14,7 @@ insert "SUM" into item 1 of csvData -- add a new column heading
// Go through all of the data rows to add the sum
repeat with rowNum= 2 to the number of items in csvData
insert the sum of item rowNum of csvData into item rowNum of csvData
insert the sum of item rowNum of csvData into item rowNum of csvData
end repeat
put csvData -- see the modified data as a list of lists

View file

@ -13,9 +13,9 @@ proc addSumColumn {filename {title "SUM"}} {
set sumcol [$m columns]
$m add column $title
for {set i 1} {$i < [$m rows]} {incr i} {
# Fill out a dummy value
$m set cell $sumcol $i 0
$m set cell $sumcol $i [tcl::mathop::+ {*}[$m get row $i]]
# Fill out a dummy value
$m set cell $sumcol $i 0
$m set cell $sumcol $i [tcl::mathop::+ {*}[$m get row $i]]
}
# Write the CSV out

View file

@ -1,6 +1,6 @@
set f [open example.csv r]
puts "[gets $f],SUM"
while { [gets $f row] > 0 } {
puts "$row,[expr [string map {, +} $row]]"
puts "$row,[expr [string map {, +} $row]]"
}
close $f

View file

@ -1,35 +0,0 @@
'Instatiate FSO.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the CSV file for reading. The file is in the same folder as the script and named csv_sample.csv.
Set objInCSV = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\csv_sample.csv",1,False)
'Set header status to account for the first line as the column headers.
IsHeader = True
'Initialize the var for the output string.
OutTxt = ""
'Read each line of the file.
Do Until objInCSV.AtEndOfStream
line = objInCSV.ReadLine
If IsHeader Then
OutTxt = OutTxt & line & ",SUM" & vbCrLf
IsHeader = False
Else
OutTxt = OutTxt & line & "," & AddElements(line) & vbCrLf
End If
Loop
'Close the file.
objInCSV.Close
'Open the same file for writing.
Set objOutCSV = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\csv_sample.csv",2,True)
'Write the var OutTxt to the file overwriting existing contents.
objOutCSV.Write OutTxt
'Close the file.
objOutCSV.Close
Set objFSO = Nothing
'Routine to add each element in a row.
Function AddElements(s)
arr = Split(s,",")
For i = 0 To UBound(arr)
AddElements = AddElements + CInt(arr(i))
Next
End Function