diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2022-10-03 14:54:39 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2022-10-03 14:54:39 +0100 |
| commit | 87577ba46615d972d7a7a4701785e49a93cceac7 (patch) | |
| tree | bf6fdd0e3d93007fecbadbb07e5094f7c1ac682e | |
| parent | 28dc2b86e008c47b91abdad519be34b4e18338f6 (diff) | |
| download | perlweeklychallenge-club-87577ba46615d972d7a7a4701785e49a93cceac7.tar.gz perlweeklychallenge-club-87577ba46615d972d7a7a4701785e49a93cceac7.tar.bz2 perlweeklychallenge-club-87577ba46615d972d7a7a4701785e49a93cceac7.zip | |
Week 185 solutions
| -rw-r--r-- | challenge-185/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-185/peter-campbell-smith/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-185/peter-campbell-smith/perl/ch-2.pl | 37 |
3 files changed, 60 insertions, 0 deletions
diff --git a/challenge-185/peter-campbell-smith/blog.txt b/challenge-185/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..de5f65dc75 --- /dev/null +++ b/challenge-185/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/10/manipulating-characters.html diff --git a/challenge-185/peter-campbell-smith/perl/ch-1.pl b/challenge-185/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..1f2cdefd68 --- /dev/null +++ b/challenge-185/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-10-02 +# PWC 185 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given MAC address in the form hhhh.hhhh.hhhh. +# Write a script to convert the address to the form hh:hh:hh:hh:hh:hh. + +# Blog: https://pjcs-pwc.blogspot.com/2022/10/manipulating-characters.html + +my (@tests, $test); + +@tests = ('1ac2.34f0.b1c2', 'abc1.20f1.345a'); + +# just do it +for $test (@tests) { + say $test =~ m|^(..)(..).(..)(..).(..)(..)$| ? qq[\nInput: $test\nOutput: $1:$2:$3:$4:$5:$6]: 'Invalid format'; +} diff --git a/challenge-185/peter-campbell-smith/perl/ch-2.pl b/challenge-185/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..b972199ad5 --- /dev/null +++ b/challenge-185/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-10-02 +# PWC 185 task 1 + +use v5.28; +use utf8; +use warnings; +binmode(STDOUT, ':utf8'); + +# You are given a list of random strings. +# Write a script to change the first four characters matching [a-z0-9] to x and keep the rest as it is. + +# Blog: https://pjcs-pwc.blogspot.com/2022/10/manipulating-characters.html + +my (@tests, $test, @list, $item, $j, $output); + +@tests = (['ab-cde-123', '123.abc.420', '3abc-0010.xy'], + ['A5h&kP.....z', '!"£$%^&*()', 'xxxx00000', 'abcd', 'ǀØΨ£‡⁇ aaaa']); + +# loop over tests +for $test (@tests) { + @list = @$test; + + # initialise + say qq[\nInput: \@list = (] . join(', ', @list) . ')'; + $output = ''; + + # loop over items within test + for $item (@list) { + $item =~ s|^(.*?)[a-z0-9](.*?)[a-z0-9](.*?)[a-z0-9](.*?)[a-z0-9]|$1x$2x$3x$4x|; + $output .= qq[$item, ]; + } + + # and print the answer + say qq[Output: (] . substr($output, 0, -2) . ')'; +} |
