[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Interleaving arrays




O.k., not particularly fun, but...


sub columnize($@) {
  my $ncols = shift;
  my @a = @_;

  my $nrows = int( ( $#a + $ncols ) / $ncols );

  for ( my $p = @a; @a % $ncols; $p -= ($nrows-1) ) {
    splice @a, $p, 0, undef;
  }

  (
    @a[
      map { 
        my $i=$_;
        map { $i+$_*$nrows } 0..$ncols-1
      } 0..$nrows-1
    ]
  )[ 0..$#_ ]
}


Note that all the undefs that got spliced into the array
in the for loop wind up at the end of the list produced
by @a[ map... ], so, if you would rather have the array
padded out, you could remove the (...)[0..$#_] thing,
returning just the @a[...] bit.

-- 
John Porter


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe