How to get the string Cartesian Products of 2 list

X

xueweizhong

You should realize that Perl is a programming language and thus does not
have all the shortcuts that bash offers. No Perl solution will satisfy
you if you just want bash. If you want bash, use bash.
I use everything I have. Why not do the programming in mutiple
lanugages which complements each other.

--Todd
 
D

Dr.Ruud

Dr.Ruud schreef:
Ted Zlatanov schreef:
local $" = ','; # List separator
print glob "{@{['a'..'c']}}{@{[1..3]}}\n";

Command line version:

perl -wle '
$"=",";
print for glob "{@{[ q{a}..q{c} ]}}{@{[ 1..3 ]}}"
'

Others:

perl -le'
sub c{local $"=","; "{@_}"}
print for glob c("a".."c").c(1..3)
'

perl -le'
sub c{"{". join(",", @_) ."}"}
print for glob c("a".."c").c(1..3)
'
 
X

xueweizhong

local $" = ','; # List separator
print glob "{@{['a'..'c']}}{@{[1..3]}}\n";

Is this difference below a language feature or bug?
perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>' a001,a002,a003,a004,a005,a006,a007,a008,a009,a010,a011,a012,a013,a014,a015,a016,a017,a018,a019,a020,a021,a022,a023,a024,a025,a026,a027,a028,a029,a030,a031,a032,a033,a034,a035,a036,a037,a038,a039,a040,a041,a042,a043,a044,a045,a046,a047,a048,a049,a050,a051,a052,a053,a054,a055,a056,a057,a058,a059,a060,a061,a062,a063,a064

perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'
a

-Todd
 
M

Michele Dondi

perl glob in perl5.8 is not power enough than the bash3.0 glob, it
doesn't support .. operator which is fatal to this issue:

I didn't know that bash 3.* did that. In any case, that's good.

In perl, you can currently use a workaround:
#!/bin/perl

sub X {split / /,qx/bash -c "echo @_"/}

print "glob:\n";
print join "\n", glob "{a..b}{1..2}";

local ($",$,)=(",", "\n");
print glob "{@{['a'..'b']}}{@{[1..2]}}"

Of course, someone may implement a bash30_glob() to add to File::Glob.


Michele
 
M

Michele Dondi

I do think these lines are worse than using loop BLOCK. What I want is
an single expression, not statements with so many nosiy lines.

You can have a single statement, but that's probably even more noisy:

my @globbed = do { local $"=',';
glob "{@{['a'..'c']}}{@{[1..3]}}" };


Michele
 
X

xueweizhong

perl -e 'local $" = " said:
Feature. Try

perl -le 'print for "aa".."bb"'
You misunderstood me. What i want to understand is the difference
between the output of 2 commands below:
1. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
2. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'

The 2nd one doesn't out of our expection, can you try this to see
what's going on?:
$perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'
a


-Todd
 
D

Dr.Ruud

(e-mail address removed) schreef:
Ruud:
glob "{@{[ q{a}..q{c} ]}}{@{[ 1..3 ]}}"

Really nice, a pure perl way now rather than dependant on my bash one.

After your inspiration, i get another solution using map:

perl -e 'print join ",", map { my $g=$_; map $g.$_,1..3 } "a".."c"'
a1,a2,a3,b1,b2,b3,c1,c2,c3

$ perl -wle'
sub mm{my $_0=shift; map {my $g=$_; map $g.$_, @_>1?mm(@_):mad:{$_[0]}}
@$_0}
print for mm ["a".."c"], [1..3], ["x".."z"], [7..9];
'
a1x7
a1x8
....
c3z8
c3z9
 
M

Michele Dondi

You misunderstood me. What i want to understand is the difference
between the output of 2 commands below:
1. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
2. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'

I'm astonished: unless I'm missing something *very* obvious, the only
difference is that the former prints up to the 064 and the second to
065.
The 2nd one doesn't out of our expection, can you try this to see
what's going on?:
$perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'
a

Aren't you by any chance just experiencing some problem with the
prompt and not printing a newline?


Michele
 
D

David Combs

Give 2 string list such as: a..b and 1..3, i want to get a list which
is (a1, a2, a3, b1, b2, b3). Is there an elegant way in perl to
fullfill this withou using loop BLOCK? Or Is there a elegant one line
expression to get this?

Lots of neat solutions to *this* problem in this thread!

Now, how about generalizing it to eg what csh, etc, has:

{a, b, c, {e, f, g}{1,2}, x}{" hello", "goodbye"} etc

How to even *approach* this problem, or even *think* about it.

A whole set of coroutines or generators or something?

Any ideas?

Thanks

David

Not that I really need a solution -- seems to me
like a very interesting problem, various possible data structures,
various possible control structures, ...
 
M

Michele Dondi

Lots of neat solutions to *this* problem in this thread!

Now, how about generalizing it to eg what csh, etc, has:

{a, b, c, {e, f, g}{1,2}, x}{" hello", "goodbye"} etc

How to even *approach* this problem, or even *think* about it.

Well Perl 6 should have that. I know because I once asked in p6l,
however I can't remember the syntax offhand, and it may have changed
in the meanwhile. Currently, we have Algorithm::Loops::NestedLoops().


Michele
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top