Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to permute multi arrays with different numbers of element?

Reply
Thread Tools

how to permute multi arrays with different numbers of element?

 
 
VZD
Guest
Posts: n/a
 
      03-09-2012
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";
}
}
}




 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2012
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
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      03-10-2012
>>>>> "Ruud" == Ruud <(E-Mail Removed)> 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
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      03-10-2012
In article <jje09l$97t$(E-Mail Removed)>, VZD <(E-Mail Removed)> 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
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-10-2012
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2012
On 2012-03-10 04:07, Ben Morrow wrote:
> Quoth "VZD"<(E-Mail Removed)>:


>> 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2012
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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-10-2012
"Dr.Ruud" <(E-Mail Removed)> wrote:
> On 2012-03-10 04:07, Ben Morrow wrote:
> > Quoth "VZD"<(E-Mail Removed)>:

>
> >> 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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2012
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2012
On 2012-03-10 17:51, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> "Dr.Ruud"<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
permute each element of a ragged array? Phlip Ruby 17 05-18-2009 04:34 PM
Randomly permute a list of integers Michael McGarry C Programming 6 08-11-2006 08:24 PM
all_sequences.pl (Was: Perl - permute?) kj Perl Misc 1 02-09-2005 04:28 AM
function to permute a string Yuan Zhong C Programming 2 08-05-2004 12:32 AM



Advertisments