Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here is some code that works very well for me.

#! /usr/bin/perl -w use strict; my @array = @ARGV ? @ARGV : qw/mix up these words in new ways/; sub fac { my $nr = shift; return 1 if $nr < 2; return $nr*fac($nr-1); } my %seen; my $max = fac(scalar @array); my $nr = 0; while( $nr < $max ) { my $need_combo = 1; my @combo; while( $need_combo ) { for( my $i = 0; $i < scalar @array; ++$i ) { $combo[$i] = $array[ rand scalar @array ]; } my %unique; my $unique = 1; for my $c ( @combo ) { if( ++$unique{$c} > 1 ) { $unique = 0; } } if( $unique == 1 ) { $need_combo = 0; } } if( exists $seen{ "@combo" } ) { next; } else { local $" = "\t"; print "@combo\n"; } ++$seen{"@combo"}; ++$nr; }

There are possibly some minor performance tweaks that could be added, but I have chosen clarity over performance. Hope this helps.

Oops, I just re-read your question. You said you wanted combinations of this array, but apparently you also want selections. Oh well, I'm sure you can figure out easily enough how to extend this program to get it to do that too.

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to Re: printing all combinations of an array.. (code supplied) by grinder
in thread printing all combinations of an array.. by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2019-12-12 09:26 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found