B
Babacio
Hi,
I have a problem that has something to do with globing or something
like that -- I don't understand it deeply...
I need a function that shuffle a list by blocks. Blocks are defined
using a subroutine given as a parameter to the function.
Well, a piece of code is better than a long description:
#---------------------------------------------------------------------
use strict;
use List::Util qw(shuffle);
sub shuffle_blocks (&\@) {
my $code = shift();
my $r_tab = shift();
my @res;
my $i = 0;
my $N = scalar(@$r_tab);
my $a = $$r_tab[0];
while($i<$N) {
my @list = ();
my $b;
while( $i<$N and &{$code}($a,$b = $$r_tab[$i]) ) {
push @list, $b;
$i++;
}
push @res, (shuffle @list);
$a = $b;
}
return @res;
}
my %hash = ('a'=>1, 'B'=>2, 'c'=>1, 'd'=>1, 'E'=>2, 'f'=>1, 'G'=>2);
my @keys = keys %hash;
my @sorted = sort { $hash{$a} <=> $hash{$b} } @keys;
print "@sorted\n";
for(my $i =1; $i<10; $i++) {
my @shuf = shuffle_blocks { $hash{$_[0]} == $hash{$_[1]} } @sorted;
print "@shuf\n";
}
#--------------------------------------------------------
The output tastes like :
c a d f G E B
a f d c G B E
c a f d B G E
f c d a B G E
d a f c G E B
c f d a B G E
f a c d B E G
f a c d G E B
a d c f E G B
f c a d B E G
I think this example makes clear what I am doing.
Now here is my question: I want to modify shuffle_blocks so that I
can use it like this:
shuffle_blocks { $hash{$a} == $hash{$b} } @sorted;
It's not very important, but if there is an easy way to do that, I'd
like to know it. I tried various things, but they behave bad if $a, $b
are already defined before the call to the function...
I have a problem that has something to do with globing or something
like that -- I don't understand it deeply...
I need a function that shuffle a list by blocks. Blocks are defined
using a subroutine given as a parameter to the function.
Well, a piece of code is better than a long description:
#---------------------------------------------------------------------
use strict;
use List::Util qw(shuffle);
sub shuffle_blocks (&\@) {
my $code = shift();
my $r_tab = shift();
my @res;
my $i = 0;
my $N = scalar(@$r_tab);
my $a = $$r_tab[0];
while($i<$N) {
my @list = ();
my $b;
while( $i<$N and &{$code}($a,$b = $$r_tab[$i]) ) {
push @list, $b;
$i++;
}
push @res, (shuffle @list);
$a = $b;
}
return @res;
}
my %hash = ('a'=>1, 'B'=>2, 'c'=>1, 'd'=>1, 'E'=>2, 'f'=>1, 'G'=>2);
my @keys = keys %hash;
my @sorted = sort { $hash{$a} <=> $hash{$b} } @keys;
print "@sorted\n";
for(my $i =1; $i<10; $i++) {
my @shuf = shuffle_blocks { $hash{$_[0]} == $hash{$_[1]} } @sorted;
print "@shuf\n";
}
#--------------------------------------------------------
The output tastes like :
c a d f G E B
a f d c G B E
c a f d B G E
f c d a B G E
d a f c G E B
c f d a B G E
f a c d B E G
f a c d G E B
a d c f E G B
f c a d B E G
I think this example makes clear what I am doing.
Now here is my question: I want to modify shuffle_blocks so that I
can use it like this:
shuffle_blocks { $hash{$a} == $hash{$b} } @sorted;
It's not very important, but if there is an easy way to do that, I'd
like to know it. I tried various things, but they behave bad if $a, $b
are already defined before the call to the function...