Perl Weekly Challenge 358.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 358.
Task 1: Max Str Value
Submitted by: Mohammad Sajid Anwar
You are given an array of alphanumeric string, @strings.
Write a script to find the max value of alphanumeric string in the given array.
The numeric representation of the string, if it comprises of digits only otherwise
length of the string.
Example 1
Input: @strings = ("123", "45", "6")
Output: 123
"123" -> 123
"45" -> 45
"6" -> 6
Example 2
Input: @strings = ("abc", "de", "fghi")
Output: 4
"abc" -> 3
"de" -> 2
"fghi" -> 4
Example 3
Input: @strings = ("0012", "99", "a1b2c")
Output: 99
"0012" -> 12
"99" -> 99
"a1b2c" -> 5
Example 4
Input: @strings = ("x", "10", "xyz", "007")
Output: 10
"x" -> 1
"xyz" -> 3
"007" -> 7
"10" -> 10
Example 5
Input: @strings = ("hello123", "2026", "perl")
Output: 2026
"hello123" -> 8
"perl" -> 4
"2026" -> 2026
I can use a regular expression to test if a string contains only
digits, in which case its value is itself. Otherwise, its value is its
length. Then I can use max from List::Util to get the maximum
value. I assume the inputs are within a space separated string. The
result fits a short one-liner.
perl -MList::Util=max -E '
for(@ARGV){say "$_ -> ", max map {/^\d+$/?$_:length}split " "}
' "123 45 6" "abc de fghi" "0012 99 a1b2c" "x 10 xyz 007" "hello123 2026 perl"
Results:
123 45 6 -> 123
abc de fghi -> 4
0012 99 a1b2c -> 99
x 10 xyz 007 -> 10
hello123 2026 perl -> 2026
The full code is similar.
1 # Perl weekly challenge 358
2 # Task 1: Max Str Value
3 #
4 # See https://wlmb.github.io/2026/01/26/PWC358/#task-1-max-str-value
5 use v5.36;
6 use List::Util qw(max);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 S0 S1...
9 where each string Sn consists of space separated substrings
10 to obtain the maximum value of those substrings. The value is
11 the numeric value for strings of digits or the length of the
12 string.
13 FIN
14 for(@ARGV){
15 say "$_ -> ", max map {/^\d+$/ ? $_ : length }split " "
16 }
Example:
./ch-1.pl "123 45 6" "abc de fghi" "0012 99 a1b2c"\
"x 10 xyz 007" "hello123 2026 perl"
Results:
123 45 6 -> 123
abc de fghi -> 4
0012 99 a1b2c -> 99
x 10 xyz 007 -> 10
hello123 2026 perl -> 2026
Task 2: Encrypted String
Submitted by: Mohammad Sajid Anwar
You are given a string $str and an integer $int.
Write a script to encrypt the string using the algorithm - for each character
$char in $str, replace $char with the $int th character after $char in the alphabet,
wrapping if needed and return the encrypted string.
Example 1
Input: $str = "abc", $int = 1
Output: "bcd"
Example 2
Input: $str = "xyz", $int = 2
Output: "zab"
Example 3
Input: $str = "abc", $int = 27
Output: "bcd"
Example 4
Input: $str = "hello", $int = 5
Output: "mjqqt"
Example 5
Input: $str = "perl", $int = 26
Output: "perl"
I make an array to map integers to letters and a hash to map letters to integers, and I use them for each letter in a string to find its index, shift it by adding the integer using modular arithmetic, and use the new index to get the corresponding new letter. Finally I join all the letters to get the output. The result fits a 1.5-liner.
perl -E '
$a{$_}=$n++ for@a="a".."z";for my($i,$s)(@ARGV){say"$i $s -> ",join "",map
{$a[($a{$_}+$i)%@a]}split "",$s}
' 1 abc 2 xyz 27 abc 5 hello 26 perl
Results:
1 abc -> bcd
2 xyz -> zab
27 abc -> bcd
5 hello -> mjqqt
26 perl -> perl
The code above is limited as it only allows lower-case letters. I lift this limitation and add a few tests to the full code. If a character is not an upper or lower case ascii, I let it through without modification.
1 # Perl weekly challenge 358
2 # Task 2: Encrypted String
3 #
4 # See https://wlmb.github.io/2026/01/26/PWC358/#task-2-encrypted-string
5 use v5.36;
6 use feature qw(try);
7 die <<~"FIN" unless @ARGV and @ARGV%2==0;
8 Usage: $0 N0 S0 N1 S1...
9 to shift the letters of the string Si by the integer Ni.
10 FIN
11 my @letter = 'a'..'z';
12 my %index_of;
13 my $n=0;
14 $index_of{$_}=$n++ for @letter;
15 for my($int, $string)(@ARGV){
16 say"$int $string -> ",
17 join "",
18 map {
19 my $upper=/[A-Z]/;
20 my $lower=/[a-z]/;
21 $upper ? uc $letter[($index_of{lc $_} + $int) % @letter]
22 : $lower? $letter[($index_of{$_} + $int) % @letter]
23 : $_
24 } split "", $string
25 }
Example:
./ch-2.pl 1 abc 2 xyz 27 abc 5 hello 26 perl
Results:
1 abc -> bcd
2 xyz -> zab
27 abc -> bcd
5 hello -> mjqqt
26 perl -> perl
Other examples:
./ch-2.pl 1 Abc 2 xYz? 27 abC 5 HELLO! 26 Perl
Results:
1 Abc -> Bcd
2 xYz? -> zAb?
27 abC -> bcD
5 HELLO! -> MJQQT!
26 Perl -> Perl
/;