#!/usr/bin/perl # run this like: # foo.pl # foo.pl 5 use strict; # get user input for the number of columns my $cols = $ARGV[0] || 2; my @images = qw(1.gif 2.gif 3.jpg 4.png 5.jpg); my $pos = 0; my $row = 0; my @rowdata; foreach my $file (sort @images) { $rowdata[$row][$pos] = qq|\n| if $pos == 0; $rowdata[$row][$pos] .= qq| $file|; if (($pos > 0 && $pos % ($cols-1) == 0) || $cols == 1) { # we just filled the last position. Reset. $rowdata[$row][$pos] .= qq|\n\n|; $pos = 0; $row++; } else { $pos++; } } unless ($pos == 0) { $rowdata[$row][$pos++] = qq|  | until $pos == $cols; $rowdata[$row][$pos] .= qq|\n|; } print join("\n",@{$_}) for @rowdata;