我有一个表文件,我想在Perl中打乱特定列的行。
例如,我有这个数组:
a 1
b 2
c 3
d 4
e 5
f 6
我想洗牌第二列以获得这样的内容:
a 2
b 1
c 3
d 4
e 5
f 6
使用List::Util::shuffle
可能是个好主意。我使用施瓦茨变换创建了一个随机数列表,对它们进行排序,并根据数组索引插入列数据。
use strict;
use warnings;
use feature 'say';
my @col;
while (<DATA>) {
push @col, [ split ];
}
my @shuffled = map { $col[$_->[0]][1] } # map to @col values
sort { $a->[1] <=> $b->[1] } # sort based on rand() value
map { [ $_, rand() ] } # each index mapped into array of index and rand()
0 .. $#col; # list of indexes of @col
for my $index (0 .. $#col) {
say join " ", $col[$index][0], $shuffled[$index];
}
__DATA__
a 1
b 2
c 3
d 4
e 5
f 6
我可以使用此脚本来完成这项工作:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw/shuffle/;
my @c = split /,/, $ARGV[0];
$_-- for @c;
shift;
my @lines;
my @v;
while ( <> ) {
my @items = split;
$v[$.-1] = [@items[@c]];
$lines[$.-1] = [@items];
}
my @order = shuffle (0..$#lines);
for my $l (0..$#lines) {
my @items = @{ $lines[$l] };
@items[@c] = @{ $lines[$order[$l]] }[@c];
print "@itemsn";
}
此脚本使用List::Util
,它是 perl v5.7.3 以来 Perl 核心模块的一部分:corelist List::Util
它可以启动perl shuffle.pl 2 test.txt
不允许使用外部模块的情况的演示代码。
use strict;
use warnings;
use feature 'say';
my %data;
while( <DATA> ) {
my($l,$d) = split;
$data{$l} = $d;
}
say '- Loaded --------------------';
say "$_ => $data{$_}" for sort keys %data;
for( 0..5 ) {
@data{ keys %data} = @{ shuffle([values %data]) };
say "-- $_ " . '-' x 24;
say "$_ => $data{$_}" for sort keys %data;
}
sub shuffle {
my $data = shift;
my($seen, $r, $i);
my $n = $#$data;
for ( 0..$n ) {
do {
$i = int(rand($n+1));
} while defined $seen->{$i};
$seen->{$i} = 1;
$r->[$_] = $data->[$i];
}
return $r;
}
__DATA__
a 1
b 2
c 3
d 4
e 5
f 6
输出
- Loaded --------------------
a => 1
b => 2
c => 3
d => 4
e => 5
f => 6
-- 0 ------------------------
a => 5
b => 4
c => 2
d => 6
e => 1
f => 3
-- 1 ------------------------
a => 3
b => 6
c => 2
d => 4
e => 1
f => 5
-- 2 ------------------------
a => 4
b => 5
c => 6
d => 1
e => 3
f => 2
-- 3 ------------------------
a => 6
b => 4
c => 1
d => 2
e => 3
f => 5
-- 4 ------------------------
a => 3
b => 4
c => 6
d => 5
e => 1
f => 2
-- 5 ------------------------
a => 6
b => 5
c => 3
d => 4
e => 2
f => 1