in reply to Long array -> multiple columns?

Here's an implementation with simpler calculations, with fewer special cases and which can easily be extended to four columns.

It also fixes some bug in yours. Yours skips the 17th element ($dupes[16]) when you have 23 elements (@dupes == 23). I didn't track down the cause of the error.

The idea is simple: Pretend the array is a 2d array, then combine the row and col indexes into one.

my @dupes = qw( a b c d e f g h i j k l m n o p q r s t u v w ); my $num_cols = int( @dupes / 10 ) + 1; if ($num_cols > 3) { $num_cols = 3; } my $num_rows = int( $#dupes / $num_cols ) + 1; for my $r ( 0 .. $num_rows-1 ) { for my $c ( 0 .. $num_cols-1 ) { my $i = $r + $c * $num_rows; last if $i > $#dupes; print("\t$dupes[$i]"); } print("\n"); }

Update: Added "The idea ..." paragraph.

Replies are listed 'Best First'.
Re^2: Long array -> multiple columns?
by azredwing (Sexton) on Jan 30, 2008 at 07:16 UTC
    This looks perfect. Thanks so much for your help!