Skip to content

Instantly share code, notes, and snippets.

@yongbin
Created June 27, 2011 13:49
Show Gist options
  • Select an option

  • Save yongbin/1048884 to your computer and use it in GitHub Desktop.

Select an option

Save yongbin/1048884 to your computer and use it in GitHub Desktop.
for jeen
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Data::Dumper;
use Test::More;
# (a1 a2 a3)(b4)(c5 c6)
=pod
$ echo {a1,a2,a3}{c5,c6}
a1c5 a1c6 a2c5 a2c6 a3c5 a3c6
$ echo {a1,a2,a3}b4
a1b4 a2b4 a3b4
perl -E "say for glob('{a,b}{c,d}')"
ac
ad
bc
bd
=cut
sub func {
my ($set) = @_;
my ( @target, @result );
foreach my $k ( sort keys %$set ) {
push @target,
[ glob( sprintf( '%s{%s}', $k, join( ',', @{ $set->{$k} } ) ) ) ];
}
while ( my $index = shift @target ) {
foreach my $item (@target) {
my @mixed = glob(
sprintf( '{%s},{%s}',
join( ',', @$index ),
join( ',', @$item ) )
);
foreach my $i (@mixed) {
my ( $a, $b ) = split( ',', $i );
push @result, [ $a, $b ];
}
}
}
return \@result;
}
{
is_deeply( func( { a => [ 1, 2 ], b => [ 3, 4 ] } ),
[ [ 'a1', 'b3' ], [ 'a1', 'b4' ], [ 'a2', 'b3' ], [ 'a2', 'b4' ] ] );
is_deeply(
func( { a => [ 1, 2, 3 ], b => [4], c => [ 5, 6 ] } ),
[
[ 'a1', 'b4' ],
[ 'a2', 'b4' ],
[ 'a3', 'b4' ],
[ 'a1', 'c5' ],
[ 'a1', 'c6' ],
[ 'a2', 'c5' ],
[ 'a2', 'c6' ],
[ 'a3', 'c5' ],
[ 'a3', 'c6' ],
[ 'b4', 'c5' ],
[ 'b4', 'c6' ],
]
);
}
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment