home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!west.West.Sun.COM!male.EBay.Sun.COM!grapevine.EBay.Sun.COM!news2me.EBay.Sun.COM!jethro.Corp.Sun.COM!animus!eric
- From: eric@animus.Corp.Sun.COM (eric )
- Newsgroups: comp.lang.perl
- Subject: Re: In search of good idioms.
- Date: 17 Dec 1992 23:53:45 GMT
- Organization: Sun Microsystems, Inc.
- Lines: 59
- Distribution: world
- Message-ID: <lj24o9INNlg@jethro.Corp.Sun.COM>
- References: <ej.724585686@id.dth.dk>
- Reply-To: eric@animus.Corp.Sun.COM
- NNTP-Posting-Host: animus.corp.sun.com
-
- >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
- >
-
- Here's one that uses lists and recursion:
-
-
- @list = ( a, b, c );
- &permute( *list );
- exit;
-
- sub permute {
- local( *list, *print_list ) = @_;
- local( @short_list, $i );
-
- if ( $#list < 0 ){
- return 0; }
-
- for $i ( $[ .. $#list )
- {
- @short_list = @list;
- # remove current element from our local list, and pass the remainder down
- push( @print_list, splice( @short_list, $i, 1 ) );
- if ( ! &permute( *short_list, *print_list ) ) {
- print join(" ", @print_list ), "\n"; }
- pop( @print_list );
- }
- return 1;
- }
-
-
-
- BTW, why doesn't this work:
-
- push( @print_list, splice( @short_list = @list, $i, 1 ) );
- ^^^^^^^
- or
- push( @print_list, splice( (@short_list = @list), $i, 1 ) );
-
- -Eric
-
- (+= all disclaimers)
-
-