aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-06-01 16:42:05 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-06-01 16:42:05 +0200
commit9f85ca9947718000cf0b0456d2a9d9e2977ec5c7 (patch)
tree6a7313fbcfe0b211f4e490dbfe9e29f602ef797c
parent338f4ac1ee158bf35e7a1710e947199e005b1df7 (diff)
downloadperlweeklychallenge-club-9f85ca9947718000cf0b0456d2a9d9e2977ec5c7.tar.gz
perlweeklychallenge-club-9f85ca9947718000cf0b0456d2a9d9e2977ec5c7.tar.bz2
perlweeklychallenge-club-9f85ca9947718000cf0b0456d2a9d9e2977ec5c7.zip
Implemented another approach.
-rw-r--r--challenge-063/luca-ferrari/raku/ch-1.p617
1 files changed, 16 insertions, 1 deletions
diff --git a/challenge-063/luca-ferrari/raku/ch-1.p6 b/challenge-063/luca-ferrari/raku/ch-1.p6
index 5bc1338035..e96cf8c939 100644
--- a/challenge-063/luca-ferrari/raku/ch-1.p6
+++ b/challenge-063/luca-ferrari/raku/ch-1.p6
@@ -17,7 +17,17 @@
sub last_word( $string, $regexp ){
+ my $last-word = Nil;
for $string.split( " " ) {
+ $last-word = $_ if $_ ~~ $regexp;
+ }
+
+ return $last-word;
+}
+
+
+sub last_word_shorter( $string, $regexp ){
+ for $string.split( " " ).reverse {
return $_ if $_ ~~ $regexp;
}
}
@@ -27,5 +37,10 @@ sub MAIN(){
say last_word(' hello world', rx/<[ea]>l/); # 'hello'
say last_word("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!'
say last_word("spaces in regexp won't match", rx:s/ in re / ); # undef
- say last_word( join(' ', 1..1e6), rx/ ^ (3.*?) ** 3 / ); # '399933'
+ say last_word( join(' ', 1..1e6), rx/ ^ (3.*?) ** 3 / ); # '399933'
+
+ say last_word_shorter(' hello world', rx/<[ea]>l/); # 'hello'
+ say last_word_shorter("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!'
+ say last_word_shorter("spaces in regexp won't match", rx:s/ in re / ); # undef
+ say last_word_shorter( join(' ', 1..1e6), rx/ ^ (3.*?) ** 3 / ); # '399933'
}