The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


String::Combination - An algorithm to calculate all the strings resulting from a combination of k characters from n characters available.


 use String::Combination qw(combination);

 my @array=combination("foo",2);

 print "$_ " foreach (@array);

 #prints of fo of fo oo oo 

 my @array=combination("bar",3);

 print "$_ " foreach (@array);

 #prints abr arb rab rba bra bar 


From a set of n elements, given an integer k, the algorithm calculates k! * n!/(k!(n-k)!) elements that are all the combinations of k elements from the set of n elements.

The combination function needs a string (every character is an element of the set of n=length(string) elements) and an integer value that is k.

The function returns a list of values.

If the function returns an empty list, an error has occured and you can retrive its description from $String::Combination::err .


The C algorithm and code is by Andrea Gasparri, the XS glue is by Dree Mistrut with a help from dada <>.

Copyright 2004 Andrea Gasparri <> and Dree Mistrut <>.

This package is free software and is provided "as is" without express or implied warranty. You can redistribute it and/or modify it under the same terms as Perl itself.