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

Re: Creating tuples based on sets

by Thelonius (Priest)
on Apr 16, 2003 at 15:36 UTC ( [id://250938]=note: print w/replies, xml ) Need Help??


in reply to Creating tuples based on sets

#!perl -w use strict; my @set = qw(a b c); my @tuples = mk_tuples(3, \@set); for (@tuples) { print "(", join(", ", @$_), ")\n"; } sub mk_tuples { my $n = shift; my $setr = shift; my $len = 0; my @result = (); my @hold = (); mk_tuples_r($n, $setr, \@hold, \@result); return @result; } sub mk_tuples_r { my ($n, $setr, $holdr, $resultr) = @_; my $len = 1 + @$holdr; for my $e (@$setr) { push @$holdr, $e; if ($len == $n) { push @$resultr, [ @$holdr ]; } else { mk_tuples_r($n, $setr, $holdr, $resultr); } pop @$holdr; } }

Replies are listed 'Best First'.
thank you!
by waxmop (Beadle) on Apr 16, 2003 at 16:08 UTC
    Your response was the best combination of easy to understand and successful. I love this website.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://250938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-29 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found