how to permute multi arrays with different numbers of element?

Discussion in 'Perl Misc' started by VZD, Mar 9, 2012.

  1. VZD

    VZD Guest

    Please help me to solve problem, how to permute multi arrays with different
    numbers of element? I can make it manually, but what If you have more that
    three arrays?

    Thanks


    # Perl permute multidimensional arrays
    #

    my @digits1 = qw(1 2);
    my @digits2 = qw(1 2);
    my @digits3 = qw(a b c);

    foreach my $i (@digits1) {
    foreach my $j (@digits2) {
    foreach my $k (@digits3) {
    print "$i$j$k$l\n";
    }
    }
    }
    VZD, Mar 9, 2012
    #1
    1. Advertising

  2. VZD

    Dr.Ruud Guest

    On 2012-03-09 23:30, VZD wrote:

    > Please help me to solve problem, how to permute multi arrays with different
    > numbers of element? I can make it manually, but what If you have more that
    > three arrays?


    Check out `perldoc -f glob`.


    > my @digits1 = qw(1 2);
    > my @digits2 = qw(1 2);
    > my @digits3 = qw(a b c);


    Numbered names raise a red flag.

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #2
    1. Advertising

  3. >>>>> "Ruud" == Ruud <> writes:

    >> my @digits1 = qw(1 2);
    >> my @digits2 = qw(1 2);
    >> my @digits3 = qw(a b c);


    Ruud> Numbered names raise a red flag.

    Some call it "code smell". I like that.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    Randal L. Schwartz, Mar 10, 2012
    #3
  4. VZD

    Alan Curry Guest

    In article <jje09l$97t$>, VZD <> wrote:
    >Please help me to solve problem, how to permute multi arrays with different
    >numbers of element? I can make it manually, but what If you have more that
    >three arrays?
    >
    >Thanks
    >
    >
    ># Perl permute multidimensional arrays
    >#
    >
    >my @digits1 = qw(1 2);
    >my @digits2 = qw(1 2);
    >my @digits3 = qw(a b c);
    >
    >foreach my $i (@digits1) {
    > foreach my $j (@digits2) {
    > foreach my $k (@digits3) {
    > print "$i$j$k$l\n";
    > }
    > }
    >}


    This operation is called a "Cartesian product". Knowing that, it should be
    easy to find several implementations.

    --
    Alan Curry
    Alan Curry, Mar 10, 2012
    #4
  5. On Sat, 10 Mar 2012 03:07:51 +0000, Ben Morrow wrote:

    > (I seem to keep finding situations where I want to be able to say
    >
    > map my $d { ... } ...
    >
    > , and I believe the 'my' is enough to stop it being ambiguous. Hmmm.)


    I like it. Makes perfect sense.

    M4
    Martijn Lievaart, Mar 10, 2012
    #5
  6. VZD

    Dr.Ruud Guest

    On 2012-03-10 04:07, Ben Morrow wrote:
    > Quoth "VZD"<>:


    >> Please help me to solve problem, how to permute multi arrays with different
    >> numbers of element? I can make it manually, but what If you have more that
    >> three arrays?

    >
    > Hmm, well, since noone's given the obvious answer yet...


    Was glob too obvious?

    perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #6
  7. VZD

    Dr.Ruud Guest

    On 2012-03-10 02:15, Randal L. Schwartz wrote:
    > Ruud:
    >> vdz:


    >>> my @digits1 = qw(1 2);
    >>> my @digits2 = qw(1 2);
    >>> my @digits3 = qw(a b c);

    >>
    >> Numbered names raise a red flag.

    >
    > Some call it "code smell". I like that.


    1. Numbered names smell.
    2. Numbered names are code smell.
    3. Numbered names make your code smell.
    4. Code with numbered names smells.

    I still prefer my n-n r-r pattern.
    (I would translate "pink panther" to "paarse panter",
    though "pink" is normally "roze".)

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #7
  8. VZD

    Guest

    "Dr.Ruud" <> wrote:
    > On 2012-03-10 04:07, Ben Morrow wrote:
    > > Quoth "VZD"<>:

    >
    > >> Please help me to solve problem, how to permute multi arrays with
    > >> different numbers of element? I can make it manually, but what If you
    > >> have more that three arrays?

    > >
    > > Hmm, well, since noone's given the obvious answer yet...

    >
    > Was glob too obvious?
    >
    > perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'


    I haven't tried this on a newer perl, but at least on older ones this
    method will get slow and beat the crap out of your hard drive as the list
    gets longs. It checks whether each string exists as a filename, but the
    results of that check are ignored.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Mar 10, 2012
    #8
  9. VZD

    Dr.Ruud Guest

    On 2012-03-10 17:10, Ben Morrow wrote:
    > Ruud:
    >> Ben:
    >>> vdz:


    >>>> Please help me to solve problem, how to permute multi arrays with
    >>>> different numbers of element? I can make it manually, but what If
    >>>> you have more that three arrays?
    >>>
    >>> Hmm, well, since noone's given the obvious answer yet...

    >>
    >> Was glob too obvious?
    >>
    >> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

    >
    > glob doesn't work with arbitrary strings. I suppose you could assume the
    > OP was only (ever going to be) permuting digits, but I generally dislike
    > solutions that involve quoting and reparsing. Besides, the question
    > asked for an arbtrary number of arrays, and I don't know that
    >
    > glob join "",
    > map "{$_}",
    > map { join ",", @$_ }
    > @digits;
    >
    > is much more concise than the solution I posted.


    Good points. It easily gets ugly:

    perl -wle '
    my @digits = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, "8,X" ] );
    print for glob join "",
    map "{$_}",
    map join( ",", map quotemeta, @$_ ),
    @digits;
    '

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #9
  10. VZD

    Dr.Ruud Guest

    On 2012-03-10 17:51, wrote:
    > "Dr.Ruud"<> wrote:


    >> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

    >
    > I haven't tried this on a newer perl, but at least on older ones this
    > method will get slow and beat the crap out of your hard drive as the list
    > gets longs. It checks whether each string exists as a filename, but the
    > results of that check are ignored.


    Why do you think it would go to disk?

    <quote src=`perldoc -f glob`>
    If non-empty braces are the only wildcard characters used in
    the "glob", no filenames are matched, but potentially many
    strings are returned.
    </quote>

    Still, see Ben's reasons to avoid glob.

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #10
  11. VZD

    Dr.Ruud Guest

    On 2012-03-10 18:33, Ben Morrow wrote:
    > Ruud:
    >> xhoster:


    >>> I haven't tried this on a newer perl, but at least on older ones this
    >>> method will get slow and beat the crap out of your hard drive as the list
    >>> gets longs. It checks whether each string exists as a filename, but the
    >>> results of that check are ignored.

    >>
    >> Why do you think it would go to disk?
    >>
    >> <quote src=`perldoc -f glob`>
    >> If non-empty braces are the only wildcard characters used in
    >> the "glob", no filenames are matched, but potentially many
    >> strings are returned.
    >> </quote>

    >
    > Let's have a look, shall we?
    >
    > ~% ktrace -tn perl -e'glob "{1,2}{3,4}"'
    > ~% kdump
    > <snip>
    > 34066 perl NAMI "13"
    > 34066 perl NAMI "14"
    > 34066 perl NAMI "23"
    > 34066 perl NAMI "24"
    > ~%
    >
    > So, yes, it goes to the disk, whether it needed to or not. (This is
    > 5.12.2.)


    Bug?

    --
    Ruud
    Dr.Ruud, Mar 10, 2012
    #11
  12. On 2012-03-10 17:13, Dr.Ruud <> wrote:
    > On 2012-03-10 17:51, wrote:
    >> "Dr.Ruud"<> wrote:

    >
    >>> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

    >>
    >> I haven't tried this on a newer perl, but at least on older ones this

    ^^^^^^^^^^
    >> method will get slow and beat the crap out of your hard drive as the list
    >> gets longs. It checks whether each string exists as a filename, but the
    >> results of that check are ignored.

    >
    > Why do you think it would go to disk?


    Because older ones *do* go to disk?

    % strace perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'
    [...]
    lstat64("147", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("148", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("157", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("158", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("167", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("168", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("247", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("248", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("257", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("258", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("267", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("268", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("347", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("348", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("357", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("358", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("367", 0xbf910610) = -1 ENOENT (No such file or directory)
    lstat64("368", 0xbf910610) = -1 ENOENT (No such file or directory)
    write(1, "147\n", 4147
    ) = 4
    write(1, "148\n", 4148
    ) = 4
    write(1, "157\n", 4157
    ) = 4
    [...]

    % perl -v

    This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi
    (with 56 registered patches, see perl -V for more detail)

    Copyright 1987-2009, Larry Wall


    ><quote src=`perldoc -f glob`>
    > If non-empty braces are the only wildcard characters used in
    > the "glob", no filenames are matched, but potentially many
    > strings are returned.
    ></quote>


    This sentence isn't there in 5.10.1. If newer perls don't try to stat
    the files any more (note that "no filenames are matched" doesn't
    necessarily imply this), then the change was made after 5.10.1

    hp


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
    Peter J. Holzer, Mar 10, 2012
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yuan Zhong

    function to permute a string

    Yuan Zhong, Aug 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    441
    CBFalconer
    Aug 5, 2004
  2. Michael McGarry

    Randomly permute a list of integers

    Michael McGarry, Aug 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    574
  3. Phlip
    Replies:
    17
    Views:
    220
    Rick DeNatale
    May 18, 2009
  4. kj
    Replies:
    1
    Views:
    130
  5. PerlFAQ Server

    FAQ 4.51 How do I permute N elements of a list?

    PerlFAQ Server, Feb 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    150
    PerlFAQ Server
    Feb 7, 2011
Loading...

Share This Page