My son (11) has just learned SI units and their prefixes at school. They started just with metres and litres, only some of the prefixes (mili, centi, deci, hecto, kilo) and they don't know the floating point yet. Because of his broken arm, he missed several days at school and needed to practice. So, I've written a Tk application for him to practice. It takes one parameter, the number of formulas to generate.

Feel free to localize it using the constants at the beginning. Adding the floating point left as an exercise for the reader.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ shuffle }; use Function::Parameters; use Tk; use constant { SEPARATOR => ',', OK => 'OK', TITLE => 'SI Units Conversions', DONE => 'Done', QUIT => 'Quit', ERRORS => 'Errors', }; push @ARGV, -font => 'sans 15'; fun prefixes (@prefixes) { +{ map +($_ => undef), "", @prefixes } } fun do_format ($x) { my $f = reverse $x; $f =~ s/(...)(?=.)/$1${\SEPARATOR}/g; return reverse $f } fun validate ($new_value, @) { $new_value =~ /^[0-9${\SEPARATOR}]*$/ } my %known = (l => prefixes(qw( m c d h )), m => prefixes(qw( m c d k ))); my %times = (m => 0.001, c => 0.01, d => 0.1, "" => 1, h => 100, k => 1000); my @exercises; for (1 .. shift) { my $unit = (keys %known)[rand keys %known]; my @prefixes = sort { $times{$a} <=> $times{$b} } (shuffle(keys %{ $known{$unit} }))[0, 1]; push @exercises, [ $unit, @prefixes, 1 + int rand 100 ]; $exercises[-1][-1] *= 10 for 1 .. rand 2; if (int rand 2) { my $times = $times{ $prefixes[1] } / $times{ $prefixes[0] }; $exercises[-1] = [ $unit, @prefixes[1, 0], $exercises[-1][-1] * $times ]; } } my (@solutions, @entries); my $errors = 0; my $mw = 'MainWindow'->new(title => TITLE); for my $i (0 .. $#exercises) { my $exercise = $exercises[$i]; my $f = $mw->Frame->pack; my $ratio = $times{ $exercise->[2] } / $times{ $exercise->[1] }; $f->Label(-text => do_format($exercise->[3] * $ratio) . " $exercise->[1]$exercise->[0] = ") ->pack(-side => 'left'); push @entries, $f->Entry(-textvariable => \$solutions[$i], -validate => 'all', -validatecommand => \&validate, )->pack(-side => 'left'); $f->Label(-text => " $exercise->[2]$exercise->[0]") ->pack(-side => 'left'); } my $f = $mw->Frame->pack; $f->Button(-text => DONE, -command => fun () { for my $i (0 .. $#solutions) { next if $entries[$i]->cget('-state') eq 'readonly'; my $value = ($solutions[$i] // "") =~ s/${\SEPARATO +R}//gr; if (($value || 0) == $exercises[$i][3]) { $entries[$i]->configure(-state => 'readonly' +, -validate => 'none'); $solutions[$i] = OK; } else { ++$errors; } } })->pack(-side => 'left'); $f->Button(-text => QUIT, -command => [$mw => 'destroy'])->pack(-side => 'left'); MainLoop(); say ERRORS, ": $errors";

It's been a practice for me, too, because of Function::Parameters.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Conversions of SI Units by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.