Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Name Me! MixMatch?

by jackdied (Monk)
on Dec 19, 2001 at 14:02 UTC ( [id://133076]=perlquestion: print w/replies, xml ) Need Help??

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

Here is a module I use for solving little problems (it's in my Dr Dobbs toolkit) and I need a better name. Someday I might throw it in with my Combination module and Permutation module (both of which I also use for Dr Dobbs type problems), and then I'll need to figure out a namespace. For now, I just need to name this little guy.
package MixMatch; use strict; =comments Takes an lref of lrefs like [ [a], [x, y, z] [1, 2] ] and returns in order [a, x, 1] [a, x, 2] [a, y, 1] [a, y, 2] [a, z, 1] [a, z, 2] =cut sub new { my $self = bless {}, shift; my %args = (-array=>undef, # lref of lrefs @_); $self->{arr} = $args{-array}; my $sizes = []; my $total = 1; foreach my $lref (@{$self->{arr}}) { push @$sizes, scalar(@$lref); $total *= $sizes->[-1]; } $self->{rows} = scalar(@{$self->{arr}}); $self->{sizes} = $sizes; $self->{count} = $total; return $self; } sub next { my $self = shift; my $c = $self->{count}--; return undef unless $c; my $ret = []; for (my $i = 0; $i < $self->{rows}; $i++) { push @$ret, $self->{arr}->[$i]->[$c % $self->{sizes}->[$i]]; } return $ret; } 1;
I won't post my Permutation.pm here, it is 4x slower than Algorithm::Permute (which is a nice piece of XS), I just use mine because I like the order of permutations is produces. And yes, this module could be implemented in terms of Permute's, but it is so short that new()ing a few other objects seems like overkill

Suggestions?

-jackdied

Replies are listed 'Best First'.
Re: Name Me! MixMatch?
by ariels (Curate) on Dec 19, 2001 at 14:20 UTC
      Very cool. If I had only know that before, I wouldn't have had to write it :p
      Also, can reading from a file handle return non-scalars?

      -jackdied

        Reading from a file handle returns either a scalar, if you're doing something like "$x = <FH>", or a list, if you're doing something like "@x = <FH>". These are the only 2 contexts (scalar context and list context) available in Perl (well, there's also a void context, if you're really picky).

        But I don't think that's what you're asking. If you meant "can reading from a file handle return non-strings", the answer is also positive. Your READLINE method can return any scalars it likes. A list reference is just a scalar, so it could return a list reference in scalar context and a list of list references in list context. Just like any Perl function, really.

Re (tilly) 1: Name Me! MixMatch?
by tilly (Archbishop) on Jan 02, 2002 at 04:40 UTC
Broken, except for trivail cases (such as the example)
by jackdied (Monk) on Dec 20, 2001 at 04:28 UTC
    This code is broken, and only does the right thing because of the 1/2/3 nature of the example. Use this patch (only a few lines have changed, so it should be human readable) to get it to work for all cases.
    RCS file: /home/perl/tools/Cartesian.pm,v retrieving revision 1.2 diff -r1.2 Cartesian.pm 30c30,31 < my $sizes = []; --- > my $div = []; > my $mod = []; 33,34c34,36 < push @$sizes, scalar(@$lref); < $total *= $sizes->[-1]; --- > push @$div, $total; > push @$mod, scalar(@$lref); > $total *= $mod->[-1]; 37,38c39,42 < $self->{sizes} = $sizes; < $self->{count} = $total; --- > $self->{div} = $div; > $self->{mod} = $mod; > $self->{total} = $total; > $self->{count} = 0; 44,45c48,49 < my $c = $self->{count}--; < return undef unless $c; --- > my $c = $self->{count}++; > return undef if ($c == $self->{total}); 48c52 < push @$ret, $self->{arr}->[$i]->[$c % $self->{sizes}->[$i]]; --- > push @$ret, $self->{arr}->[$i]->[($c / $self->{div}->[$i]) % + $self->{mod}->[$i]];
    Woops
Re: Name Me! MixMatch?
by chip (Curate) on Dec 19, 2001 at 14:08 UTC
    If you're the only one who's going to use it, why not give it a name only you would recognize?

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Name Me! MixMatch?
by blakem (Monsignor) on Jan 15, 2002 at 05:26 UTC
    Perhaps you're unfamiliar with this glob trick?
    perl -le 'print for glob("{a}:{x,y,z}:{1,2}")' a:x:1 a:x:2 a:y:1 a:y:2 a:z:1 a:z:2

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found