############################################################# # Generates all combinations of a given word. A word must be # atleast 2 characters long. This script finds usefullness in # solving 'jumble words' that usually appears in newspapers # # Copyright (C) 2007 Srivatsa Kanchi, R # License: GPL # ############################################################## use strict; use vars qw/$usage/; $usage = <<_EOF_; USAGE: perl -w AllCombs.pl Outputs all combinations of characters in a given word (input a word of 2 or more characters). _EOF_ # sub gen_allpairs: makes a recursive call to generate all the # combinatons of characters of a given word sub gen_allpairs { my ($i, $j, $t, @inp_lst); ($i, @inp_lst) = @_; $j =$i; if( ($i+1) == $#inp_lst ) { print @inp_lst, "\n"; $t = $inp_lst[$i]; $inp_lst[$i] = $inp_lst[$i+1]; $inp_lst[$i+1] = $t; print @inp_lst, "\n"; return; } while(1) { gen_allpairs($i+1, @inp_lst); $j++; last if ($j > $#inp_lst); $t = $inp_lst[$i]; $inp_lst[$i] = $inp_lst[$j]; $inp_lst[$j] = $t; } } # main do { print $usage; exit} if($#ARGV); do { print "Error: Input a word of 2 or more characters\n\n"; exit} if(length $ARGV[0] < 2); gen_allpairs(0, split //, $ARGV[0]);