-
-
Save dbohdan/387712fe67abb3774f17 to your computer and use it in GitHub Desktop.
Data munging task from the Perl 6 advent calendar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Peter B | |
Celine A- | |
Zsófia B+ | |
João F | |
Maryam B+ | |
秀英 B- | |
Finn D+ | |
Aarav A | |
Emma F | |
Omar B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An outdated version of the code for https://dbohdan.com/wiki/data-munging.