Skip to content

Instantly share code, notes, and snippets.

@dbohdan
Forked from smls/grades.p6
Last active July 23, 2021 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbohdan/387712fe67abb3774f17 to your computer and use it in GitHub Desktop.
Save dbohdan/387712fe67abb3774f17 to your computer and use it in GitHub Desktop.
Data munging task from the Perl 6 advent calendar
#!/usr/bin/env tclsh
package require sqlite3
package require fileutil
sqlite3 db :memory:
db eval {CREATE TABLE grades(name TEXT PRIMARY KEY, grade TEXT)}
foreach {name grade} [::fileutil::cat grades.txt] {
if {![regexp {[A-F][+-]?} $grade]} {
puts "Can't parse pair '$name $grade'"
exit 1
}
db eval {INSERT INTO grades VALUES ($name, $grade)}
}
puts "Zsófia's grade: [db eval {SELECT grade FROM grades WHERE name='Zsófia'}]"
puts "List of students with a failing grade:"
puts " [join [
db eval {SELECT name FROM grades WHERE grade >= 'E' ORDER BY grade}
] {, }]"
set grades [db eval {
SELECT DISTINCT trim(grade, '+-') FROM grades ORDER BY grade
}]
puts "Distribution of grades by letter:"
foreach grade $grades {
puts -nonewline " $grade: "
set count [db eval {
SELECT count(name) FROM grades WHERE trim(grade, '+-')=$grade
}]
puts -nonewline "$count student"
if {$count > 1} { puts -nonewline s }
puts {}
}
Peter B
Celine A-
Zsófia B+
João F
Maryam B+
秀英 B-
Finn D+
Aarav A
Emma F
Omar B
Zsófia's grade: B+
List of students with a failing grade:
João, Emma
Distribution of grades by letter:
A: 2 students
B: 5 students
D: 1 student
F: 2 students
@dbohdan
Copy link
Author

dbohdan commented Jul 23, 2021

An outdated version of the code for https://dbohdan.com/wiki/data-munging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment