Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

printing all combinations of an array..

by Anonymous Monk
on Feb 19, 2002 at 18:47 UTC ( #146419=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,
here's my problem:

I have an array of less than 6 strings, and I want to print all combinations of these strings.
@words = (nvidia,GeForce,2,MX);
this should print out:
nvidia GeForce 2 MX
nvidia 2 MX
nvidia GeForce MX
nvidia MX
nvidia 2 GeForce
nvidia MX 2
nvidia MX GeForce
GeForce nvidia
GeForce nvidia 2
GeForce nvidia MX
GeForce 2 nvidia

please help me find a solution to this, I'd be so grateful :)
  • Comment on printing all combinations of an array..

Replies are listed 'Best First'.
Re: printing all combinations of an array..
by tachyon (Chancellor) on Feb 20, 2002 at 00:44 UTC
Re: printing all combinations of an array..
by dragonchild (Archbishop) on Feb 19, 2002 at 18:57 UTC
    Use SuperSearch. This question has been asked every month for the past 18 months. :-)

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      It's been discussed before that Super Search is rather tough to find on the PM site, and I agree. Hence, there's your link.

      Title edit by tye as Super Search should find only one match

        It used to be really easy to find under Super Search, until somebody made another node with that name!
Re: printing all combinations of an array..
by YuckFoo (Abbot) on Feb 19, 2002 at 20:50 UTC
    In the spirit of TIMTOWTDI, here's my solution.

    For n items there are 2^n combinations. Create a binary string for each combination number. Check each digit of the binary string to determine if the corresponding item gets printed.


    #!/usr/bin/perl use strict; my (@list) = qw(one two three four); printcombo(\@list); #----------------------------------------------------------- sub printcombo { my ($list) = @_; my (@print, $str, $i, $j); my $size = @{$list}; for ($i = 0; $i < 2**$size; $i++) { $str = sprintf("%*.*b", $size, $size, $i); @print = (); for ($j = 0; $j < $size; $j++) { if (substr($str, $j, 1)) { push (@print, $list->[$j]); } } print join(' ', @print) . "\n"; } }
      In the spirit of TIMTOWDI?


      That is the spirit of the lazy troll supporter ... providing solutions to FAQ questions easily found when so many have already answered them.

      Teach them How to Read The Friendly Manual.

      Sinners Repent!

Re: printing all combinations of an array.. (code supplied)
by grinder (Bishop) on Feb 20, 2002 at 10:00 UTC

    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'
Re: printing all combinations of an array..
by Anonymous Monk on Feb 20, 2002 at 12:16 UTC
    Thanx for all the help gang.
    Really fast response :)

    I apologize if you thought I was lazy and didn't check your site for previous solutions to my problem.

    The thing is, I can hardly navigate your site at all.
    Most of the time it looks like I get timed out, and some times it looks as if my browser won't handle the "spacy" nodes.
    I had to bookmark my "problem" to be able to get back to it.

    but thanx again, all of you. You've been of great help :)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://146419]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2022-01-17 06:21 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.