How to resolve the algorithm CSV data manipulation step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CSV data manipulation step by step in the EchoLisp programming language

Table of Contents

Problem Statement

CSV spreadsheet files are suitable for storing tabular data in a relatively portable way. The CSV format is flexible but somewhat ill-defined.
For present purposes, authors may assume that the data fields contain no commas, backslashes, or quotation marks.

Read a CSV file, change some values and save the changes back to a file. For this task we will use the following CSV file: Suggestions

Let's start with the solution:

Step by Step solution about How to resolve the algorithm CSV data manipulation step by step in the EchoLisp programming language

Source code in the echolisp programming language

;; CSV -> LISTS
(define (csv->row line)  (map (lambda(x) (or (string->number x) x)) (string-split line ",")))
(define (csv->table csv) (map  csv->row (string-split csv "\n")))

;; LISTS -> CSV
(define (row->csv row) (string-join row ","))
(define (table->csv header rows)
    (string-join  (cons (row->csv header) (for/list ((row rows)) (row->csv row))) "\n"))


(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))))))


(define file.csv #<<
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
>>#)

(task file.csv)

 → "C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60"


  

You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Maple programming language
You may also check:How to resolve the algorithm FASTA format step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the PowerBASIC programming language