Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Creating tuples based on sets

by The Mad Hatter (Priest)
on Apr 16, 2003 at 14:55 UTC ( #250917=note: print w/replies, xml ) Need Help??


in reply to Creating tuples based on sets

You'll have to have multiple foreach loops that go through each item in the set. For tuples of size 2, you'll need two. For tuples of size 3, you'll need three. You get the idea. You might be able to use Algorithm::Loops to help you with this.

Replies are listed 'Best First'.
Re^2: Creating tuples based on sets (nested loops)
by tye (Sage) on Apr 16, 2003 at 17:35 UTC

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2021-02-28 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?