aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-23 20:49:38 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-26 15:38:39 +0100
commitcaef8be004348b8426f7694d75a0f28eeae8b493 (patch)
tree8b50ee29407a5ae8ee2fbb9b789548bd90c4d8b2
parent191f5f72dd22cf8872b8c4d32eaebf9773becb96 (diff)
downloadperlweeklychallenge-club-caef8be004348b8426f7694d75a0f28eeae8b493.tar.gz
perlweeklychallenge-club-caef8be004348b8426f7694d75a0f28eeae8b493.tar.bz2
perlweeklychallenge-club-caef8be004348b8426f7694d75a0f28eeae8b493.zip
Solution to task 1
-rwxr-xr-xchallenge-253/jo-37/perl/ch-1.pl62
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-253/jo-37/perl/ch-1.pl b/challenge-253/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..70212982a6
--- /dev/null
+++ b/challenge-253/jo-37/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples, $sep);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless defined $sep && @ARGV;
+usage: $0 [-examples] [-tests] [-sep=S STR...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-sep=C
+ use C as separator
+
+STR
+ list of strings
+
+EOS
+
+
+### Input and Output
+
+say join ',', map qq("$_"), split_strings($sep, @ARGV);
+
+
+### Implementation
+
+sub split_strings {
+ my $sep = shift;
+ grep length, map +(split /\Q$sep/), @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [split_strings('.' => "one.two.three","four.five","six")],
+ ["one","two","three","four","five","six"], 'example 1';
+ is [split_strings('$' => '$perl$$', '$$raku')],
+ ["perl","raku"], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [split_strings(0 => qw(00aa00b00 00c00dd00))],
+ [qw(aa b c dd)], 'empty fields with zero separator';
+ }
+
+ done_testing;
+ exit;
+}