Perl Weekly Challenge 330.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 330.

Task 1: Clear Digits

Submitted by: Mohammad Sajid Anwar
You are given a string containing only lower case English letters and digits.

Write a script to remove all digits by removing the first digit and the closest
non-digit character to its left.


Example 1
Input: $str = "cab12"
Output: "c"

Round 1: remove "1" then "b" => "ca2"
Round 2: remove "2" then "a" => "c"

Example 2
Input: $str = "xy99"
Output: ""

Round 1: remove "9" then "y" => "x9"
Round 2: remove "9" then "x" => ""

Example 3
Input: $str = "pa1erl"
Output: "perl"

A repeated substitution ought to work, yielding a very simple half-liner.

Examples:

perl -E '
for(@ARGV){$i=$_; 1 while s/[^0-9][0-9]//; say "$i -> $_"}
' cab12 xy99 pa1erl

Results:

cab12 -> c
xy99 ->
pa1erl -> perl

It is not clear what ought to be done if there is no non-digit character to the left of a digit. The code above keeps the digit in those cases. The code above doesn’t check that the non-digits are lowercase English letters.

Example:

perl -E '
for(@ARGV){$i=$_; 1 while s/[^0-9][0-9]//; say "$i -> $_"}
' 2a3 a23 !#3

Results:

2a3 -> 2
a23 -> 3
!#3 -> !

The full code only adds some checks.

 1  # Perl weekly challenge 330
 2  # Task 1:  Clear Digits
 3  #
 4  # See https://wlmb.github.io/2025/07/14/PWC330/#task-1-clear-digits
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 S1 S2...
 8      to repeatedly remove digits from the string Sn, together with the closest
 9      non-digit to its left.
10      Only lowercase letters and digits are allowed in the strings.
11      FIN
12  for(@ARGV){
13      my $input=$_;
14      say("Only digits and lowercase letters allowed: $input"), next
15          unless /^[a-z0-9]*$/;
16      1 while s/[a-z][0-9]//;  #strip all letter-digit pairs
17      say("Couldn't strip all digits: $input -> $_"), next if /[0-9]/;
18      say "$input -> $_";
19  }

Example:

./ch-1.pl cab12 xy99 pa1erl 2a3 a23 !#3

Results:

cab12 -> c
xy99 ->
pa1erl -> perl
Couldn't strip all digits: 2a3 -> 2
Couldn't strip all digits: a23 -> 3
Only digits and lowercase letters allowed: !#3

Task 2: Title Capital

Submitted by: Mohammad Sajid Anwar
You are given a string made up of one or more words separated by a
single space.

Write a script to capitalise the given title. If the word length is 1
or 2 then convert the word to lowercase otherwise make the first
character uppercase and remaining lowercase.


Example 1
Input: $str = "PERL IS gREAT"
Output: "Perl is Great"

Example 2
Input: $str = "THE weekly challenge"
Output: "The Weekly Challenge"

Example 3
Input: $str = "YoU ARE A stAR"
Output: "You Are a Star"

I first convert everything to lowercase, Then I use Perl’s shortcut operators to convert to titlecase if the length is larger than 2. The result fits a one-liner.

Examples:

perl -E '
say join " ", map {(length($_) >2 && ucfirst)||$_} split " ", lc for @ARGV
' "PERL IS gREAT" "THE weekly challenge" "YoU ARE A stAR"

Results:

Perl is Great
The Weekly Challenge
You Are a Star

The full code is:

 1  # Perl weekly challenge 330
 2  # Task 2:  Title Capital
 3  #
 4  # See https://wlmb.github.io/2025/07/14/PWC330/#task-2-title-capital
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 S1 S2...
 8      to convert words in the space-separated strings Sn to titlecase
 9      or to lowercase if their length exceeds or not 2 characters.
10      FIN
11  say join " ", map {(length($_) >2 && ucfirst)||$_} split " ", lc for @ARGV;

Example:

./ch-2.pl "PERL IS gREAT" "THE weekly challenge" "YoU ARE A stAR"

Results:

Perl is Great
The Weekly Challenge
You Are a Star

/;

Written on July 14, 2025