in reply to A vertical (+/- random) split of a file

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:
#!/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__
HTH

Replies are listed 'Best First'.
Re^2: A vertical (+/- random) split of a file
by Marsel (Sexton) on Jul 22, 2006 at 05:33 UTC
    Thanks !
    That's great. But just to be sure, my loop
    unless($perm++) { #Do the permutation }
    was done only once, wasn't it ?

    Because, at first line, $perm is undef so evaluation of $perm++ is FALSE (and incremented) so the permutation is done, but after that, $perm++ is TRUE, so it isn't executed ? Is this wrong ? I'm quite sure it worked because a column was shuffled as a whole with that ??

    But your script is really cool, and i didn't know i could make the output like that !

    Marsel
      Yes, your use of $perm worked correctly it's just that I am more used to using $.