home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!mcsun!sunic!dkuug!iesd.auc.dk!uts!id.dth.dk!ej
- From: ej@id.dth.dk (Erik Johansen (EMP))
- Subject: Re: In search of good idioms.
- Message-ID: <ej.724585686@id.dth.dk>
- Sender: news@id.dth.dk (USENET News System)
- Organization: Department of Computer Science
- References: <BzCw1H.H24@NeoSoft.com>
- Date: Thu, 17 Dec 1992 09:48:06 GMT
- Lines: 54
-
- claird@NeoSoft.com (Cameron Laird) writes:
-
- >I'm a newbie perl-plucker. I'm working through some
- >exercises I've set myself, in an effort to develop
- >some taste in matters of perl style. Current problem:
- >write a permute program. Such a program would, for
- >example, when presented with input
-
- > permute a1 zebra third
-
- >generate output something like
-
- > a1 zebra third
- > a1 third zebra
- > zebra a1 third
- > zebra third a1
- > third a1 zebra
- > third zebra a1
-
- Sure, I wrote a piece of code that should do that:
-
- ------- Cut here --------
- #!/usr/local/bin/perl
- print "Permuting (", join(" ", @ARGV), ")\n";
-
- # Count first down by one so we don't skip first solution
- $index[ $pos = $#ARGV ]--; # So @index is (0,0,0..., -1)
-
- permutation:
- while ( $pos >= 0 )
- {
- $index[$pos--] = 0, next if ++$index[ $pos ] > $#ARGV;
-
- undef @used; # Check to see if number occurs twice, then loop
- foreach $idx ( @index )
- { $pos = $#ARGV, next permutation if $used[ $idx ]++; }
-
- print join(" ", @ARGV[ @index ]), "\n";
- $pos = $#ARGV;
- }
- ------- Cut here --------
-
- I tried it out and it gives the output you requested.
-
-
- Hope this helps
- Erik Johansen
-
- ---
- $txt=" ltrterhnuc--sor eep-\nkJ.a "; srand(53747414);
- for (1..26) { print substr($txt,rand 27,1); } ### Is this a random write ?
-
- Erik Johansen / Institute for Computer Science / Danish Technical University
- ej@id.dth.dk
-