comments and improvements are welcomed.I added some error checking and moved the slice calculation so it is only performed once instead of for every line of the input file:
HTH#!/usr/bin/perl # Split a tab-delimited text files into n files, repeating # first column and shuffling columns. # Julien Textoris use strict; use warnings; #Thanks to Q&A for this ! sub melange_fy { my $tab = shift; for ( my $i = @$tab; --$i; ) { my $j = int rand( $i + 1 ); next if $i == $j; @$tab[ $i, $j ] = @$tab[ $j, $i ]; } } @ARGV == 2 or die "usage: $0 file N\n\n\tN is the number of new files +to create.\n\n"; my ( $file, $n ) = @ARGV; open IN, '<', $file or die "open '$file' $!"; my @fhs = map { open my $out, '>', "$file.$_" or die "open '$file.$_' $!"; $out; } 0 .. $n - 1; my @ind; while ( <IN> ) { tr/\r\n//d; my ( $id, @elmt ) = split /\t/; #make the permutation of tab indices only the first time if ( $. == 1 ) { die "N is too large, N must be less than " . ( @elmt + 1 ) . " +.\n" if $n > @elmt; my @temp = 0 .. $#elmt; melange_fy( \@temp ); @ind = map [ $_ == $n ? @temp : splice @temp, 0, int( @elmt / +$n + 0.5 ) ], 1 .. $n; } for my $i ( 0 .. $#fhs ) { print { $fhs[ $i ] } join( "\t", $id, @elmt[ @{ $ind[ $i ] } ] + ), "\n"; } } __END__
In reply to Re: A vertical (+/- random) split of a file
by jwkrahn
in thread A vertical (+/- random) split of a file
by Marsel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |