RogerBW's Blog

Perl Weekly Challenge 33 08 November 2019

I’ve been doing the Perl Weekly Challenges This week’s was about counting letters and generating a multiplication table.

Create a script that accepts one or more files specified on the command-line and count the number of times letters appeared in the files.

Even before I've finished reading I've broken this down mentally into "turn each line into letters, then for each one add to the count for that letter". Thus:

my %o;

while (<>) {
  my $a=lc($_);
  $a =~ s/[^a-z]//g;
  map {$o{$_}++} split '',$a;

foreach my $k (sort keys %o) {
  print "$k: $o{$k}\n";

This hits several of the syntax differences in Perl6.

my %o;

for lines() {
  my $a=lc($_);
  $a ~~ s:g /<-[a .. z]>//;
  map {%o{$_}++}, split '',$a;

for sort keys %o -> $k {
  print "$k: %o{$k}\n";

Write a script to print 11x11 multiplication table, only the top half triangle.

On a terminal window, this is a job for printf.

my $n=11;
my $m1=length($n)+1;
my $m2=length($n*$n)+1;
my $fmt='%'.$m1.'s%1s'.(('%'.$m2.'s') x $n)."\n";
printf($fmt,'-' x $m1,'+',('-' x $m2) x $n);
foreach my $row (1..$n) {
  my @a=($row,'|');
  foreach my $column (1..$n) {
    if ($column < $row) {
      push @a,'';
    } else {
      push @a,$row*$column;

And Perl6 works similarly, except that length() is no longer used.

my $n=11;
my $m1=$n.chars+1;
my $m2=($n*$n).chars+1;
my $fmt='%' ~ $m1 ~ 's%1s' ~ (('%' ~ $m2 ~ 's') xx $n) ~ "\n";


But that was too easy. Why not do it in Postscript as well? The hard bit is getting the numbers out into string form so that they can be displayed – and, oddly, there's no right-align option. (I could have used stringwidth, but I was lazy.)

/n 11 def
/scale 25 def
/fs 12 def

20 650 translate

/Helvetica findfont
fs scalefont setfont

1 1 n {
    /x exch def
    /ns 30 string def
    x ns cvs
    x scale mul dup 0 moveto ns show
    neg 0 exch moveto ns show
    1 1 x {
        /y exch def
        /nv 30 string def
        x y mul nv cvs pop
        x scale mul y neg scale mul moveto nv show
    } for
} for

0 scale 2 div neg fs 2 div add moveto
scale n 1 add mul scale 2 div neg fs 2 div add lineto
scale 2 div fs 2 div add fs moveto
scale 2 div fs 2 div add scale n 1 add mul neg fs add lineto


Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1