On Tue, 14 Mar 2000, Zaigui Wang wrote:

> Just wondering if I can recursively call a sub in perl?
> 

of course.  just make darn sure that you declare all internal
variables local with the "my" command, or great chaos will
ensue.

for the fun of it, here's a recursive routine i wrote once
upon a time for generating all anagrams of a single word.

#!/usr/bin/perl

#  anagrams:  generate all anagrams of the argument word

sub generateAnagrams {
        my ($word) = $_[0] ;
        my (@anagrams, $temp) ;

        @anagrams = () ;
        return ($word) if length($word) == 1 ; # trivial condition

        foreach $ch (split(//,$word)) {
                $temp = $word ;
                $temp =~ s/$ch// ;
                foreach $newWord (generateAnagrams($temp)) {
                        push (@anagrams, $ch . $newWord) ;
                }
        }
        return @anagrams ;
}

print join("\n", generateAnagrams($ARGV[0])), "\n" ;


rday

p.s.  no nitpicking.  i was young.  and foolish.


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to