-
Notifications
You must be signed in to change notification settings - Fork 0
comb
James E Keenan edited this page Feb 28, 2016
·
3 revisions
comb() is a function (or method) which, in certain respects, is the mirror-image of split().
my ($str, $comb, @rv);
$str = 'This is a string to be split';
say q|$str: |, $str;
$comb = 'This is a string to be combed';
say q|$comb: |, $comb;
say '';
say "split on multiple-character delimiter -- 'tri' -- found in string:";
say q|split('tri', $str);|;
@rv = split('tri', $str); print "<$_>" for @rv; print "\n";
say '';
say "comb on multiple-character string matcher -- 'tri' -- found in string:";
say q|comb('tri', $comb);|;
@rv = comb('tri', $comb); print "<$_>" for @rv; print "\n";
say '';
say "comb on multiple-character pattern matcher -- /tri/ -- found in string:";
say q|comb(/tri/, $comb);|;
@rv = comb(/tri/, $comb); print "<$_>" for @rv; print "\n";
say '';
say "/[tri]/ produces same output as above; why?";
say q|comb(/[tri]/, $comb);|;
@rv = comb(/[tri]/, $comb); print "<$_>" for @rv; print "\n";
say '';
say "but /<[tri]>/ behaves more like a character class";
say q|comb(/<[tri]>/, $comb);|;
@rv = comb(/<[tri]>/, $comb); print "<$_>" for @rv; print "\n";
say '';
say "integer '1' as matcher for comb()";
say q|comb(1, $comb);|;
@rv = comb(1, $comb); print "<$_>" for @rv; print "\n";
say '';
say "integer '7' as matcher for comb()";
say q|comb(7, $comb);|;
@rv = comb(7, $comb); print "<$_>" for @rv; print "\n";
say '';
say "limit the number of fields returned by comb()";
say q|comb(7, $comb, 2);|;
@rv = comb(7, $comb, 2); print "<$_>" for @rv; print "\n";
say '';
say "method format";
say q|$comb.comb(7, 2);|;
@rv = $comb.comb(7, 2); print "<$_>" for @rv; print "\n";
say '';
say '';
my $ncomb = 'This is a different string to be combed triumphantly';
say q|$ncomb: |, $ncomb;
say '';
say "comb on multiple-character string matcher -- 'tri' -- found in string:";
say q|comb('tri', $ncomb);|;
@rv = comb('tri', $ncomb); print "<$_>" for @rv; print "\n";
say '';
say "comb on multiple-character pattern matcher -- 'tri' -- found in string:";
say q|comb(/tri/, $ncomb);|;
@rv = comb(/tri/, $ncomb); print "<$_>" for @rv; print "\n";
say '';
say "integer '1' as matcher for comb()";
say q|comb(1, $ncomb);|;
@rv = comb(1, $ncomb); print "<$_>" for @rv; print "\n";
say '';
say "integer '7' as matcher for comb()";
say q|comb(7, $ncomb);|;
@rv = comb(7, $ncomb); print "<$_>" for @rv; print "\n";
say '';
say "limit the number of fields returned by comb()";
say q|comb(7, $ncomb, 2);|;
@rv = comb(7, $ncomb, 2); print "<$_>" for @rv; print "\n";
say '';
say "method format";
say q|$ncomb.comb('tri');|;
@rv = $ncomb.comb('tri'); print "<$_>" for @rv; print "\n";
Output:
$str: This is a string to be split
$comb: This is a string to be combed
split on multiple-character delimiter -- 'tri' -- found in string:
split('tri', $str);
<This is a s><ng to be split>
comb on multiple-character string matcher -- 'tri' -- found in string:
comb('tri', $comb);
<tri>
comb on multiple-character pattern matcher -- /tri/ -- found in string:
comb(/tri/, $comb);
<tri>
/[tri]/ produces same output as above; why?
comb(/[tri]/, $comb);
<tri>
but /<[tri]>/ behaves more like a character class
comb(/<[tri]>/, $comb);
<i><i><t><r><i><t>
integer '1' as matcher for comb()
comb(1, $comb);
<T><h><i><s>< ><i><s>< ><a>< ><s><t><r><i><n><g>< ><t><o>< ><b><e>< ><c><o><m><b><e><d>
integer '7' as matcher for comb()
comb(7, $comb);
<This is>< a stri><ng to b><e combe><d>
limit the number of fields returned by comb()
comb(7, $comb, 2);
<This is>< a stri>
method format
$comb.comb(7, 2);
<This is>< a stri>
$ncomb: This is a different string to be combed triumphantly
comb on multiple-character string matcher -- 'tri' -- found in string:
comb('tri', $ncomb);
<tri><tri>
comb on multiple-character pattern matcher -- 'tri' -- found in string:
comb(/tri/, $ncomb);
<tri><tri>
integer '1' as matcher for comb()
comb(1, $ncomb);
<T><h><i><s>< ><i><s>< ><a>< ><d><i><f><f><e><r><e><n><t>< ><s><t><r><i><n><g>< ><t><o>< ><b><e>< ><c><o><m><b><e><d>< ><t><r><i><u><m><p><h><a><n><t><l><y>
integer '7' as matcher for comb()
comb(7, $ncomb);
<This is>< a diff><erent s><tring t><o be co><mbed tr><iumphan><tly>
limit the number of fields returned by comb()
comb(7, $ncomb, 2);
<This is>< a diff>
method format
$ncomb.comb('tri');
<tri><tri>