#!/usr/bin/perl -w use strict; use Data::Dumper; our @qqq=(1..100); sub print_col { my ($rows, $qref)=@_; my @cols=(); my $count=0; while($count<=$#$qref) { #This for loop creates the 2D array for (0..$rows-1) { push @{$cols[$_]}, $qref->[$count]; $count++; #This makes sure to stop when we run out of data last if $count > $#$qref; } } #Data::Dumper just in here for testing, love that module #print Dumper(\@cols); #This for loop prints it, notice I use the quoted array syntax #which causes print to output the array with spaces in between # each element. However, this is the perfect place to put #another for loop or a map statement and printf each element #with the correct width and other formatting you want. for (0..$#cols){ print "@{$cols[$_]}\n"; } } print_col(shift, \@qqq); ###################### ## Output $ ./testcols.pl 25 1 26 51 76 2 27 52 77 3 28 53 78 4 29 54 79 5 30 55 80 6 31 56 81 7 32 57 82 8 33 58 83 9 34 59 84 10 35 60 85 11 36 61 86 12 37 62 87 13 38 63 88 14 39 64 89 15 40 65 90 16 41 66 91 17 42 67 92 18 43 68 93 19 44 69 94 20 45 70 95 21 46 71 96 22 47 72 97 23 48 73 98 24 49 74 99 25 50 75 100