2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
3
Task/String-matching/Perl-6/string-matching-1.pl6
Normal file
3
Task/String-matching/Perl-6/string-matching-1.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$haystack.starts-with($needle) # True if $haystack starts with $needle
|
||||
$haystack.contains($needle) # True if $haystack contains $needle
|
||||
$haystack.ends-with($needle) # True if $haystack ends with $needle
|
||||
3
Task/String-matching/Perl-6/string-matching-2.pl6
Normal file
3
Task/String-matching/Perl-6/string-matching-2.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
|
||||
so $haystack ~~ / $needle / # True if $haystack contains $needle
|
||||
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle
|
||||
2
Task/String-matching/Perl-6/string-matching-3.pl6
Normal file
2
Task/String-matching/Perl-6/string-matching-3.pl6
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
|
||||
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle
|
||||
1
Task/String-matching/Perl-6/string-matching-4.pl6
Normal file
1
Task/String-matching/Perl-6/string-matching-4.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
my @subs = (
|
||||
# Regex-based:
|
||||
sub R_contains ( Str $_, Str $s2 ) { ? m/ $s2 / },
|
||||
sub R_starts_with ( Str $_, Str $s2 ) { ? m/ ^ $s2 / },
|
||||
sub R_ends_with ( Str $_, Str $s2 ) { ? m/ $s2 $ / },
|
||||
|
||||
# Index-based:
|
||||
sub I_contains ( Str $_, Str $s2 ) { .index( $s2) .defined },
|
||||
sub I_starts_with ( Str $_, Str $s2 ) { my $m = .index( $s2); $m.defined and $m == 0 },
|
||||
sub I_ends_with ( Str $_, Str $s2 ) { my $m = .rindex($s2); $m.defined and $m == .chars - $s2.chars },
|
||||
|
||||
# Substr-based:
|
||||
sub S_starts_with ( Str $_, Str $s2 ) { .substr(0, $s2.chars) eq $s2 },
|
||||
sub S_ends_with ( Str $_, Str $s2 ) { .substr( *-$s2.chars) eq $s2 },
|
||||
|
||||
# Optional tasks:
|
||||
sub R_find ( Str $_, Str $s2 ) { $/.from if /$s2/ },
|
||||
sub R_find_all ( Str $_, Str $s2 ) {
|
||||
my @p = .match: /$s2/, :g;
|
||||
@p».from if @p;
|
||||
},
|
||||
);
|
||||
|
||||
my $str1 = 'abcbcbcd';
|
||||
my @str2s = < ab bc cd zz >;
|
||||
|
||||
say "'$str1' vs:".fmt('%15s '), @str2s.fmt('%-15s');
|
||||
for [1, 4, 6], [2, 5, 7], [0, 3], [8, 9] {
|
||||
say();
|
||||
for @subs[.list] -> $sub {
|
||||
say "{$sub.name}:".fmt('%15s '),
|
||||
@str2s.map({ ~$sub.($str1, $_) }).fmt('%-15s');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue