For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > print all combinations









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author print all combinations
Dan Jacobson

2004-05-16, 4:31 pm

Let's say our country's radio callsigns look like the output of
for $a('B'){for $b(qw{V X}){for $c(1..8){for $d('A'..'Z'){print "$a$b$c$d\n"}}}}
It bothers me that even though I can describe those callsigns
with regexp /B[VX][1-8][A-Z]/, in printing them out one needs to write
for loops for each element. Isn't there some
sub combinations(){print all the combinations of all the elements of
the array (not permutations)} that I can write?
Chris Charley

2004-05-16, 10:31 pm

Dan Jacobson <jidanni@jidanni.org> wrote in message news:<871xlmxwmd.fsf@jidanni.org>...
> Let's say our country's radio callsigns look like the output of
> for $a('B'){for $b(qw{V X}){for $c(1..8){for $d('A'..'Z'){print "$a$b$c$d\n"}}}}
> It bothers me that even though I can describe those callsigns
> with regexp /B[VX][1-8][A-Z]/, in printing them out one needs to write
> for loops for each element. Isn't there some
> sub combinations(){print all the combinations of all the elements of
> the array (not permutations)} that I can write?


You can use this feature of glob(), (or <> ).

for(<{B}{V,X}{1,2,3,4,5,6,7,8}{A,B,C,D,E,F,G}> ) {
print "$_\n";
}

Note that I don't have the full alphabet and only A to G for demo
purposes.
Thats because the full alphabet wouldn't fit on one line here in
Google. But if you supply the full alphabet, it will print all 416
combinations
(1 * 2 * 8 * 26).

HTH
Chris
Rick Nakroshis

2004-05-17, 12:31 am

[posted and mailed]

charley@pulsenet.com (Chris Charley) wrote in
news:4f7ed6d.0405161748.31dc7732@posting.google.com:

> You can use this feature of glob(), (or <> ).
>
> for(<{B}{V,X}{1,2,3,4,5,6,7,8}{A,B,C,D,E,F,G}> ) {
> print "$_\n";
> }
>
> Note that I don't have the full alphabet and only A to G for demo
> purposes.
> Thats because the full alphabet wouldn't fit on one line here in
> Google. But if you supply the full alphabet, it will print all 416
> combinations
> (1 * 2 * 8 * 26).


Now *that* is an elegant solution. How did you come up with that? Is it
buried in the Perl documentation, somewhere?

Rick
Michele Dondi

2004-05-17, 9:31 am

On 16 May 2004 18:48:00 -0700, charley@pulsenet.com (Chris Charley)
wrote:

>You can use this feature of glob(), (or <> ).
>
>for(<{B}{V,X}{1,2,3,4,5,6,7,8}{A,B,C,D,E,F,G}> ) {
> print "$_\n";
> }
>
>Note that I don't have the full alphabet and only A to G for demo
>purposes.
>Thats because the full alphabet wouldn't fit on one line here in
>Google. But if you supply the full alphabet, it will print all 416


Well, but then, if one *really* wants to use glob(), there's a nice
interpolating facility:

#!/usr/bin/perl -l
# ...
{
local $"=',' ;
print for glob "B{V,X}{@{[1..8]}}{@{['A'..'Z']}}";
}


PS: We're *not* "here in google" ;-)


Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
Michele Dondi

2004-05-17, 9:31 am

On Sat, 15 May 2004 10:16:58 +0800, Dan Jacobson <jidanni@jidanni.org>
wrote:

>Let's say our country's radio callsigns look like the output of
>for $a('B'){for $b(qw{V X}){for $c(1..8){for $d('A'..'Z'){print "$a$b$c$d\n"}}}}


BTW: surprising as it may be $a, $b are not (well, *may not be*) good
as generic purpose variables. See 'perldoc perlvar'.

>It bothers me that even though I can describe those callsigns
>with regexp /B[VX][1-8][A-Z]/, in printing them out one needs to write
>for loops for each element. Isn't there some
>sub combinations(){print all the combinations of all the elements of
>the array (not permutations)} that I can write?


Well, as another poster suggested, you can use glob() for that, but
IMHO it's an overkill, and not particularly elegant.

I'd rather go the multiple 'for', or map() for what it matters, way.
Though if you find yourself doing this kind of things quite frequently
you may roll your own sub for this kind of things. In one case I
did...


Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
David K. Wall

2004-05-17, 11:36 am

Dan Jacobson <jidanni@jidanni.org> wrote:

> Let's say our country's radio callsigns look like the output of
> for $a('B'){for $b(qw{V X}){for $c(1..8){for $d('A'..'Z'){print
> "$a$b$c$d\n"}}}} It bothers me that even though I can describe
> those callsigns with regexp /B[VX][1-8][A-Z]/, in printing them
> out one needs to write for loops for each element. Isn't there
> some sub combinations(){print all the combinations of all the
> elements of the array (not permutations)} that I can write?


Here's one way.

sub combos {
return @{$_[0]} if @_ == 1;
my $aref = shift;
my @result;
for my $element ( combos(@_) ) {
push @result, map { $_ . $element } @$aref;
}
return @result;
}

print join "\n", combos(['B'], ['V','X'], [1..8], ['A'..'Z']);


This seems familar. I suspect there has been a previous thread about
it here sometime, but I don't feel like searching Google for it.

David K. Wall

2004-05-17, 11:36 am

I wrote:

[snip]
> This seems familar.


s/familar/familiar/

Randal L. Schwartz

2004-05-17, 12:35 pm

>>>>> "Dan" == Dan Jacobson <jidanni@jidanni.org> writes:

Dan> Let's say our country's radio callsigns look like the output of
Dan> for $a('B'){for $b(qw{V X}){for $c(1..8){for $d('A'..'Z'){print
Dan> "$a$b$c$d\n"}}}} It bothers me that even though I can describe
Dan> those callsigns with regexp /B[VX][1-8][A-Z]/, in printing them
Dan> out one needs to write for loops for each element. Isn't there
Dan> some sub combinations(){print all the combinations of all the
Dan> elements of the array (not permutations)} that I can write?

my @all_combos = glob
join "",
map "{".join(",", @$_)."}",
['B'], ['V', 'X'], [1..8], ['A'..'Z'];

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Dan Jacobson

2004-05-17, 9:31 pm

Thanks. It seems these
perl -wle '$"=",";print for <B{V,X}{@{[1..8]}}{@{[A..Z]}}>'
perl -wle '$"=",";print for glob "B{V,X}{@{[1..8]}}{@{[A..Z]}}"'
are about as simple as it gets for me to print out that section of
Taiwan amateur radio callsigns. <> it turns out is merely the
filename expander described on man perlop I/O Operators section.
I find ranges needn't have quotes for capital letters.
Anno Siegel

2004-05-18, 8:38 am

Dan Jacobson <jidanni@jidanni.org> wrote in comp.lang.perl.misc:
> Thanks. It seems these
> perl -wle '$"=",";print for <B{V,X}{@{[1..8]}}{@{[A..Z]}}>'
> perl -wle '$"=",";print for glob "B{V,X}{@{[1..8]}}{@{[A..Z]}}"'
> are about as simple as it gets for me to print out that section of
> Taiwan amateur radio callsigns. <> it turns out is merely the
> filename expander described on man perlop I/O Operators section.


> I find ranges needn't have quotes for capital letters.


Only when you are running without full strictures. Under strict "subs",
barewords must be keywords or sub names, everything else is a syntax
error. Switch on strict and quote them.

Anno
Chris Charley

2004-05-19, 1:31 pm

Michele Dondi <bik.mido@tiscalinet.it> wrote in message news:<5e6ha0hno4antrinkn4ro3bf303hfc24do@4ax.com>...

[snip]

>
> #!/usr/bin/perl -l
> # ...
> {
> local $"=',' ;
> print for glob "B{V,X}{@{[1..8]}}{@{['A'..'Z']}}";
> }
>
>
> PS: We're *not* "here in google" ;-)


Yes, of course. I realize many do not access Usenet through Google :-)
Many nice solutions posted.

Chris
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com