#!/usr/bin/perl -w use strict; use Carp; sub columnize { my $rows_ref = shift; my $num_cols = shift; my $order = shift; if (!defined $rows_ref or !defined $num_cols or !defined $order) { croak("Undefined params not permitted:\n"); } if ($num_cols < 1) {croak "Bad column-number param: $num_cols\n";} if ($order !~ /^(across|down)$/i) {croak "Bad order param: $order\n";} use integer; # use integer division for all division operations my $num_rows = ($#$rows_ref / $num_cols) + 1; my @result_rows = (); for (my $row = 0; $row < $num_rows; $row++) { for (my $col = 0; $col < $num_cols; $col++) { my $index; if ($order eq "across") { $index = ($row * $num_cols) + $col; } else { $index = ($col * $num_rows) + $row; } # Skip any empty spots in the last row (if "across") # or in the last column (if "down"): next if ($index > $#$rows_ref); $result_rows[$row]->{COLUMN_LOOP}->[$col] = $rows_ref->[$index]; } } return \@result_rows; } ____