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

In reply to Re: A vertical (+/- random) split of a file by jwkrahn
in thread A vertical (+/- random) split of a file by Marsel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.