Peter’s blog ✴ Week 364 ✴ 9 March 2026

THE WEEKLY CHALLENGE
Weird encodings

The Perl Camel

Task 1

Decrypt string

You are given a string formed by digits and '#'. Write a script to map the given string to English lowercase characters following the given rules:

  • Characters 'a' to 'i' are represented by '1' to '9' respectively.
  • Characters 'j' to 'z' are represented by '10#' to '26#' respectively.

Examples


Example 1
Input: $str = "10#11#12"
Output: "jkab"
10# -> j
11# -> k
1   -> a
2   -> b

Example 2
Input: $str = "1326#"
Output: "acz"
1   -> a
3   -> c
26# -> z

Example 3
Input: $str = "25#24#123"
Output: "yxabc"
25# -> y
24# -> x
1   -> a
2   -> b
3   -> c

Example 4
Input: $str = "20#5"
Output: "te"
20# -> t
5   -> e

Example 5
Input: $str = "1910#26#"
Output: "aijz"
1   -> a
9   -> i
10# -> j
26# -> z

Analysis

This is easily done with two regexes. The 'e' modifier - like s|x|$y|e - is slower than a fixed regex as it has to be parsed at run time each time it's encountered, but for a task like this I doubt that is significant.

Equally, I'd avoid using this if you're working for MI5, but I expect you knew that.

Perl Weekly’s review

from Perl Weekly issue 764

This post shares Peter's solutions to Perl Weekly Challenge 364, presenting clear and well-structured Perl implementations for both tasks. It explains the reasoning behind the approach and walks the reader through the logic step by step, making the solutions easy to follow. Overall, it is a solid and educational write-up that demonstrates practical Perl problem-solving and clean coding style.

Try it 

Try running the script with any input:



example: 16#518#12#

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-03-09
use utf8;     # Week 364 - task 1 - Decrypt string
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

decrypt_string('10#11#12');
decrypt_string('1326#');
decrypt_string('25#24#123');
decrypt_string('20#5');
decrypt_string('1910#26#');

sub decrypt_string {
    
    my ($string, $base);
    
    # initialise
    $string = $_[0];
    say qq[\nInput:  '$string'];
    $base = ord('a') - 1;
    
    # apply rules
    $string =~ s|([12][0-9])#|chr($base + $1)|ge;
    $string =~ s|([1-9])|chr($base + $1)|ge;
    
    say qq[Output: '$string'];
}

8 lines of code

Output from script


Input:  '10#11#12'
Output: 'jkab'

Input:  '1326#'
Output: 'acz'

Input:  '25#24#123'
Output: 'yxabc'

Input:  '20#5'
Output: 'te'

Input:  '1910#26#'
Output: 'aijz'

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain