Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,88 @@
|
|||
# count occurrances of a char in string #
|
||||
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
|
||||
OD;
|
||||
count
|
||||
END;
|
||||
|
||||
# split string on separator #
|
||||
PROC char split = (STRING str, CHAR sep) FLEX[]STRING :
|
||||
BEGIN
|
||||
INT strlen := UPB str, cnt := 0;
|
||||
INT len, p;
|
||||
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
|
||||
OD;
|
||||
IF cnt = 0 THEN list[cnt +:= 1] := str
|
||||
ELIF start <= UPB str + 1 THEN list[cnt +:= 1] := str[start:]
|
||||
FI;
|
||||
list
|
||||
END;
|
||||
|
||||
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]
|
||||
OD;
|
||||
str
|
||||
ELSE
|
||||
""
|
||||
FI;
|
||||
|
||||
# read a line from file #
|
||||
PROC readline = (REF FILE f) STRING:
|
||||
BEGIN
|
||||
STRING line;
|
||||
get (f, line); new line (f);
|
||||
line
|
||||
END;
|
||||
|
||||
# Add one item to tuple #
|
||||
OP +:= = (REF FLEX[]STRING tuple, STRING item) VOID:
|
||||
BEGIN
|
||||
[UPB tuple+1]STRING new;
|
||||
new[:UPB tuple] := tuple;
|
||||
new[UPB new] := item;
|
||||
tuple := new
|
||||
END;
|
||||
|
||||
# convert signed number TO INT #
|
||||
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
|
||||
OD;
|
||||
n * sign
|
||||
END;
|
||||
|
||||
OP STR = (INT i) STRING: whole (i,0);
|
||||
|
||||
# The main program #
|
||||
FILE foo;
|
||||
open (foo, "CSV_data_manipulation.data", stand in channel);
|
||||
FLEX[0]STRING header := char split (readline (foo), ",");
|
||||
header +:= "SUM";
|
||||
print ((join (header, ","), new line));
|
||||
WHILE NOT end of file (foo) DO
|
||||
FLEX[0]STRING fields := char split (readline (foo), ",");
|
||||
INT sum := 0;
|
||||
FOR i TO UPB fields DO
|
||||
sum +:= TOINT fields[i]
|
||||
OD;
|
||||
fields +:= STR sum;
|
||||
print ((join (fields, ","), new line))
|
||||
OD;
|
||||
close (foo)
|
||||
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.ada
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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;
|
||||
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.ada
Normal file
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.ada
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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;
|
||||
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.ada
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
(defun csv_to_nested_list (filename seperator)
|
||||
(defun csv-to-nested-list (filename seperator)
|
||||
"Reads the csv to a nested lisp list, where each sublist represents a line.
|
||||
Each line is read in as a string, the commas are substituted by spaces and
|
||||
parantheses are added to the beginning and the end. Then the string can be interpreted by the
|
||||
|
|
@ -13,23 +13,23 @@ First line is assumed to be a comment (as no comment syntax is specified)."
|
|||
;; throw away first line, which is assumed to be a comment
|
||||
(cdr list))))
|
||||
|
||||
(defun calc_sums (nested_list)
|
||||
(defun calc-sums (nested-list)
|
||||
"Return a list of sums of each sub-list in a nested list."
|
||||
(loop for sublist in nested_list collect (apply #'+ sublist)))
|
||||
(loop for sublist in nested-list collect (apply #'+ sublist)))
|
||||
|
||||
(defun list_to_csv (nested_list)
|
||||
(defun list-to-csv (nested-list)
|
||||
"Converts the nested list back into a csv-formatted string."
|
||||
(substitute #\, #\
|
||||
(substitute #\newline #\)
|
||||
(remove #\((string-trim ")(" (format nil "~A" nested_list))))))
|
||||
(remove #\((string-trim ")(" (format nil "~A" nested-list))))))
|
||||
;; main program
|
||||
;; prints the results as lisp lists and as csv
|
||||
(let ((nested_list (csv_to_nested_list "example_comma_csv.txt" #\,))
|
||||
(sum_list nil)
|
||||
(let ((nested-list (csv-to-nested-list "example_comma_csv.txt" #\,))
|
||||
(sum-list nil)
|
||||
(comment "#C1,C2,C3,C4,C5,SUM"))
|
||||
(setf sum_list (loop
|
||||
for list in nested_list
|
||||
for sum in (calc_sums nested_list)
|
||||
(setf sum-list (loop
|
||||
for list in nested-list
|
||||
for sum in (calc-sums nested-list)
|
||||
collect (append list (list sum))))
|
||||
(format t "~A~%~%" sum_list) ;; print nested list in lisp representation
|
||||
(format t "~A~%~A~%" comment (list_to_csv sum_list))) ;; print it again as csv
|
||||
(format t "~A~%~%" sum-list) ;; print nested list in lisp representation
|
||||
(format t "~A~%~A~%" comment (list-to-csv sum-list))) ;; print it again as csv
|
||||
|
|
|
|||
|
|
@ -2,78 +2,57 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lshortfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Open the sample file given.
|
||||
csvFile, err := os.Open("sample.csv")
|
||||
rows := readSample()
|
||||
appendSum(rows)
|
||||
writeChanges(rows)
|
||||
}
|
||||
|
||||
// Exit on error.
|
||||
func readSample() [][]string {
|
||||
f, err := os.Open("sample.csv")
|
||||
if err != nil {
|
||||
log.Fatal("Error opening sample csv file:", err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Make sure the file is closed before the function returns.
|
||||
defer csvFile.Close()
|
||||
|
||||
// Create a new csv reader for the file.
|
||||
csvReader := csv.NewReader(csvFile)
|
||||
|
||||
// Create an output file.
|
||||
outputFile, err := os.Create("output.csv")
|
||||
rows, err := csv.NewReader(f).ReadAll()
|
||||
f.Close()
|
||||
if err != nil {
|
||||
log.Fatal("Error creating output file:", err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
return rows
|
||||
}
|
||||
|
||||
csvWriter := csv.NewWriter(outputFile)
|
||||
defer csvWriter.Flush()
|
||||
|
||||
// For each row in the data.
|
||||
for i := 0; ; i++ {
|
||||
record, err := csvReader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal("Error reading record:", err)
|
||||
}
|
||||
|
||||
// Skip header row.
|
||||
if i == 0 {
|
||||
err = csvWriter.Write(record)
|
||||
if err != nil {
|
||||
log.Fatal("Error writing record to output file:", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// For each cell in the row.
|
||||
for cell := range record {
|
||||
// Convert value to integer for manipulation.
|
||||
v, err := strconv.Atoi(record[cell])
|
||||
if err != nil {
|
||||
log.Fatal("Error parsing cell value:", err)
|
||||
}
|
||||
|
||||
// Do something to the value.
|
||||
v += 1
|
||||
// Store the new value back in the record variable.
|
||||
record[cell] = strconv.Itoa(v)
|
||||
}
|
||||
|
||||
// Write modified record to disk.
|
||||
err = csvWriter.Write(record)
|
||||
if err != nil {
|
||||
log.Fatal("Error writing record to output file:", err)
|
||||
}
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
31
Task/CSV-data-manipulation/Java/csv-data-manipulation-3.java
Normal file
31
Task/CSV-data-manipulation/Java/csv-data-manipulation-3.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
public static void main(String[] args) throws IOException {
|
||||
|
||||
// 1st, config the CSV reader with line separator
|
||||
CsvParserSettings settings = new CsvParserSettings();
|
||||
settings.getFormat().setLineSeparator("\n");
|
||||
|
||||
// 2nd, config the CSV reader with row processor attaching the bean definition
|
||||
BeanListProcessor<Employee> rowProcessor = new BeanListProcessor<Employee>(Employee.class);
|
||||
settings.setRowProcessor(rowProcessor);
|
||||
|
||||
// 3rd, creates a CSV parser with the configs
|
||||
CsvParser parser = new CsvParser(settings);
|
||||
|
||||
// 4th, parse all rows from the CSF file into the list of beans you defined
|
||||
parser.parse(new FileReader("/examples/employees.csv"));
|
||||
List<Employee> resolvedBeans = rowProcessor.getBeans();
|
||||
|
||||
// 5th, Store, Delete duplicates, Re-arrange the words in specific order
|
||||
// ......
|
||||
|
||||
// 6th, Write the listed of processed employee beans out to a CSV file.
|
||||
CsvWriterSettings writerSettings = new CsvWriterSettings();
|
||||
|
||||
// 6.1 Creates a BeanWriterProcessor that handles annotated fields in the Employee class.
|
||||
writerSettings.setRowWriterProcessor(new BeanWriterProcessor<Employee>(Employee.class));
|
||||
|
||||
// 6.2 persistent the employee beans to a CSV file.
|
||||
CsvWriter writer = new CsvWriter(new FileWriter("/examples/processed_employees.csv"), writerSettings);
|
||||
writer.processRecords(resolvedBeans);
|
||||
writer.writeRows(new ArrayList<List<Object>>());
|
||||
}
|
||||
14
Task/CSV-data-manipulation/Julia/csv-data-manipulation.julia
Normal file
14
Task/CSV-data-manipulation/Julia/csv-data-manipulation.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
ifn = "csv_data_manipulation_in.dat"
|
||||
ofn = "csv_data_manipulation_out.dat"
|
||||
|
||||
ifile = open(ifn, "r")
|
||||
(a, h) = readcsv(ifile, Int, header=true)
|
||||
close(ifile)
|
||||
|
||||
a = hcat(a, sum(a, 2))
|
||||
h = hcat(h, "SUM")
|
||||
a = vcat(h, a)
|
||||
|
||||
ofile = open(ofn, "w")
|
||||
writecsv(ofile, a)
|
||||
close(ofile)
|
||||
11
Task/CSV-data-manipulation/TXR/csv-data-manipulation.txr
Normal file
11
Task/CSV-data-manipulation/TXR/csv-data-manipulation.txr
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
@(coll)@{name /[^,]+/}@(end)
|
||||
@(collect :vars (value sum))
|
||||
@ (bind sum 0)
|
||||
@ (coll)@{value /[^,]+/}@(set sum @(+ sum (int-str value)))@(end)
|
||||
@(end)
|
||||
@(output)
|
||||
@ (rep)@name,@(last)SUM@(end)
|
||||
@ (repeat)
|
||||
@ (rep)@value,@(last)@sum@(end)
|
||||
@ (end)
|
||||
@(end)
|
||||
35
Task/CSV-data-manipulation/VBScript/csv-data-manipulation.vb
Normal file
35
Task/CSV-data-manipulation/VBScript/csv-data-manipulation.vb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
'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
|
||||
Loading…
Add table
Add a link
Reference in a new issue