home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7506 < prev    next >
Encoding:
Internet Message Format  |  1992-12-18  |  1.7 KB

  1. 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
  2. From: eric@animus.Corp.Sun.COM (eric )
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: In search of good idioms.
  5. Date: 17 Dec 1992 23:53:45 GMT
  6. Organization: Sun Microsystems, Inc.
  7. Lines: 59
  8. Distribution: world
  9. Message-ID: <lj24o9INNlg@jethro.Corp.Sun.COM>
  10. References: <ej.724585686@id.dth.dk>
  11. Reply-To: eric@animus.Corp.Sun.COM
  12. NNTP-Posting-Host: animus.corp.sun.com
  13.  
  14. >claird@NeoSoft.com (Cameron Laird) writes:
  15. >
  16. >>I'm a newbie perl-plucker.  I'm working through some
  17. >>exercises I've set myself, in an effort to develop
  18. >>some taste in matters of perl style.  Current problem:
  19. >>write a permute program.  Such a program would, for
  20. >>example, when presented with input
  21. >
  22. >>    permute a1 zebra third
  23. >
  24. >>generate output something like
  25. >
  26. >>    a1 zebra third
  27. >>    a1 third zebra
  28. >>    zebra a1 third
  29. >>    zebra third a1
  30. >>    third a1 zebra
  31. >>    third zebra a1
  32. >
  33.  
  34. Here's one that uses lists and recursion:
  35.  
  36.  
  37. @list = ( a, b, c  );
  38. &permute( *list );
  39. exit;
  40.  
  41. sub permute {
  42.   local( *list, *print_list ) = @_;
  43.   local( @short_list, $i );
  44.  
  45.   if ( $#list < 0 ){
  46.     return 0; }
  47.     
  48.   for $i ( $[ .. $#list )
  49.   {
  50.     @short_list = @list;
  51.     # remove current element from our local list, and pass the remainder down
  52.     push( @print_list, splice( @short_list, $i, 1 ) );
  53.     if ( ! &permute( *short_list, *print_list ) ) {
  54.       print join(" ", @print_list ), "\n"; }
  55.     pop( @print_list );
  56.   }
  57.   return 1;
  58. }
  59.  
  60.  
  61.  
  62. BTW, why doesn't this work:
  63.  
  64.     push( @print_list, splice( @short_list = @list, $i, 1 ) );
  65.                            ^^^^^^^
  66. or
  67.     push( @print_list, splice( (@short_list = @list), $i, 1 ) );
  68.  
  69. -Eric
  70.  
  71. (+= all disclaimers)
  72.  
  73.