routine comb

Documentation for routine comb assembled from the following types:

class Cool

From Cool

(Cool) routine comb

Defined as:

multi sub comb(Regex $matcherStr(Cool$input$limit = * --> Seq)
multi method comb(Regex $matcher$limit = * --> Seq)

Returns all (or if supplied, at most $limit) matches of the invocant (method form) or the second argument (sub form) against the Regex as a list of strings.

say "6 or 12".comb(/\d+/).join("");           # OUTPUT: «6, 12␤» 

class Str

From Str

(Str) routine comb

multi sub    comb(Str:D   $matcherStr:D $input$limit = Inf)
multi sub    comb(Regex:D $matcherStr:D $input$limit = InfBool :$match)
multi sub    comb(Int:D $sizeStr:D $input$limit = Inf)
multi method comb(Str:D $input:)
multi method comb(Str:D $input: Str:D   $matcher$limit = Inf)
multi method comb(Str:D $input: Regex:D $matcher$limit = InfBool :$match)
multi method comb(Str:D $input: Int:D $size$limit = Inf)

Searches for $matcher in $input and returns a list of non-overlapping matches limited to at most $limit matches. If $matcher is a Regex, each Match object is converted to a Str, unless $match is set.

If no matcher is supplied, a list of characters in the string is returned, as if the matcher was rx/./.

Examples:

say "abc".comb.perl;                 # OUTPUT: «("a", "b", "c").Seq␤» 
say 'abcdefghijk'.comb(3).perl;      # OUTPUT: «("abc", "def", "ghi", "jk").Seq␤» 
say 'abcdefghijk'.comb(32).perl;   # OUTPUT: «("abc", "def").Seq␤» 
say comb(/\w/"a;b;c").perl;        # OUTPUT: «("a", "b", "c").Seq␤» 
say comb(/\N/"a;b;c").perl;        # OUTPUT: «("a", ";", "b", ";", "c").Seq␤» 
say comb(/\w/"a;b;c"2).perl;     # OUTPUT: «("a", "b").Seq␤» 
say comb(/\w\;\w/"a;b;c"2).perl# OUTPUT: «("a;b",).Seq␤» 
say comb(/.<(.)>/"<>[]()").perl;   # OUTPUT: «(">", "]", ")").Seq␤» 

If the matcher is an integer value, comb behaves as if the matcher was rx/ . ** {1..$matcher} /, but which is optimized to be much faster.

Note that a Regex matcher may control which portion of the matched text is returned by using features which explicitly set the top-level capture.

class IO::Handle

From IO::Handle

(IO::Handle) method comb

Defined as:

method comb(IO::Handle:D: Bool :$close|args --> Seq:D)

Read the handle and processes its contents the same way Str.comb does, taking the same arguments, closing the handle when done if $close is set to a true value. Implementations may slurp the file in its entirety when this method is called.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open;
say "The file has {+$fh.comb: '':close} ♥s in it";

class IO::CatHandle

From IO::CatHandle

(IO::CatHandle) method comb

Defined as:

method comb(IO::CatHandle:D: |args --> Seq:D)

Read the handle and processes its contents the same way Str.comb does, taking the same arguments. Implementations may slurp the contents of all the source handles in their entirety when this method is called.

(my $f1 = 'foo'.IO).spurt: 'foo';
(my $f2 = 'bar'.IO).spurt: 'bar';
IO::CatHandle.new($f1$f2).comb(2).perl.say;
# OUTPUT: «("fo", "ob", "ar").Seq␤» 

class IO::Path

From IO::Path

(IO::Path) method comb

Defined as:

method comb(IO::Path:D: |args --> Seq:D)

Opens the file and processes its contents the same way Str.comb does, taking the same arguments. Implementations may slurp the file in its entirety when this method is called.