A number of strings
Weekly challenge 358 — 26 January 2026
Week 358: 26 Jan 2026
You are given an array of alphanumeric strings, @strings.
Write a script to find the max value of the strings in the given array. The value is the numeric representation of the string if it comprises of digits only, otherwise it is the 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
Well, not too hard, this one, but a few points to note.
What's the best way to check whether the string is made up of only digits? I think that:
$string =~ m|^[0-9]+$|
is hard to beat. The regex will bale out as soon as it finds a character which is not one of the digits 0 to 9, and given the context that seems to be the minimum amount of work needed.
Another option is to use \d instead of [0-9],
and for most purposes that will be just as good, but
\d matches any character that Unicode classes as a
digit in any writing system, so for example each of these:
٥۵߅५৫੫૫୫௫౫೫൫๕໕ represents the number 5. And who knows,
maybe that's what Mohammad expected us to match - but I
think not.
The challenge says alphanumeric strings, but I have generalised to any strings, including empty ones or ones containing spaces or multi-byte Unicode characters.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-01-26 use utf8; # Week 358 - task 1 - Max str value use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; max_str_value('abc', 'de', 'fghi'); max_str_value('0012', '99', 'a1b2c'); max_str_value('hello123', '2026', 'perl'); max_str_value('', '00000', 'x y'); max_str_value('1', '002', '4.0', ''); max_str_value(1, 2, 'ÄäÖöÜüß'); max_str_value('5', 'gold', 'rings', '4', 'calling', 'birds', 3, 'French', 'hens', 2, 'turtle', 'doves'); max_str_value('0', ''); sub max_str_value { my (@strings, @max, $string, $value); # initialise @strings = @_; @max = (-1, ''); # loop over values for $string (@strings) { $value = $string =~ m|^[0-9]+$| ? $string : length($string); @max = ($value, $string) if $value > $max[0]; } # report say qq[\nInput: ('] . join(q[', '], @strings) . q[')]; say qq[Output: $max[0] ('$max[1]')]; }
Input: ('abc', 'de', 'fghi') Output: 4 ('fghi') Input: ('0012', '99', 'a1b2c') Output: 99 ('99') Input: ('hello123', '2026', 'perl') Output: 2026 ('2026') Input: ('', '00000', 'x y') Output: 3 ('x y') Input: ('1', '002', '4.0', '') Output: 3 ('4.0') Input: ('1', '2', 'ÄäÖöÜüß') Output: 7 ('ÄäÖöÜüß') Input: ('5', 'gold', 'rings', '4', 'calling', 'birds', '3', 'French', 'hens', '2', 'turtle', 'doves') Output: 7 ('calling') Input: ('0', '') Output: 0 ('0')
Any content of this website which has been created by Peter Campbell Smith is in the public domain