Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Creating tuples based on sets (nested loops)

by tye (Sage)
on Apr 16, 2003 at 17:35 UTC ( #250979=note: print w/replies, xml ) Need Help??

in reply to Re: Creating tuples based on sets
in thread Creating tuples based on sets

Exactly what I was thinking. (:

sub tuples { my( $count, @set )= @_; my @out; nestedLoops( [ ([0..$#set]) x $count ], { Code => sub { push @out, [@set[@_]] } } ); return \@out; } my $tuples= tuples( 2, qw(A B C) ); for my $tuple ( @$tuples ) { print "@$tuple\n"; }
or, if you want to take advantage of iterators:
sub tupleMaker { my( $count, @set )= @_; my $iter= nestedLoops( [ ([0..$#set]) x $count ] ); return sub { @set[ $iter->() ] }; } my $gen= tupleMaker( 2, qw(A B C) ); while( my @tuple= $gen->() ) { print "@tuple\n"; }
which means you can deal with tons of tuples without needing tons of RAM.

                - tye

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2021-03-03 11:55 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (77 votes). Check out past polls.