Of course I know. You need to learn about 2D arrays and how perl uses array refs to do them. Then it is as easy as:

@array1 = qw(one two); @array2 = qw(three four five); @array3 = qw(Just another Perl Hacker !); # make a 2D array my @aoa = ( \@array1, \@array2, \@array3 ); # calculate max rows ( ie find array with largest number of members ) my $max_rows = 0; for my $array_ref (@aoa) { $max_rows = @$array_ref if @$array_ref > $max_rows; } # the number of columns is the number of array refs in @aoa; my $col_num = @aoa; # we need to decide on the column width. I will use an arbitrary width + and # trim the items in each column to fit if required but you could check + the # length of every item first and set the width to length(longest_item) + + 1 my $width = 8; my $template = '%' . $width . 's'; for my $row ( 0 .. $max_rows -1 ) { for my $col ( 0 .. $col_num -1 ) { my $item = defined $aoa[$col][$row] ? $aoa[$col][$row] : ''; # for the colums to line up we need to pack them to the same w +idth # we will use printf to do this but you could use length and t +he x op # we set the $template to right justified, 8 chars wide column +s # we need to trim the $item to $width -1 chars max if we want +discrete # columns without items touching with no whitespace $item = substr $item, 0, $width -1; printf $template, $item; } print "\n"; } __DATA__ one three Just two four another five Perl Hacker !

Note that if you set the $width to say 4 you get this:

one thr Jus two fou ano fiv Per Hac !

The truncation of the items maintains the discrete columns. Here is how to do a 'fit columns to data' which I just happened to have kicking around:

@array1 = qw(one two); @array2 = qw(three four five); @array3 = qw(Just another Perl Hacker !); # make a 2D array my @aoa = ( \@array1, \@array2, \@array3 ); # calculate max rows ( ie find array with largest number of members ) my $max_rows = 0; for my $array_ref (@aoa) { $max_rows = @$array_ref if @$array_ref > $max_rows; } # the number of columns is the number of array refs in @aoa; my $col_num = @aoa; my $width = get_col_width_templates(\@aoa, 1); for my $row ( 0 .. $max_rows -1 ) { for my $col ( 0 .. $col_num -1 ) { my $item = defined $aoa[$col][$row] ? $aoa[$col][$row] : ''; # for the colums to line up we need to pack them to the same w +idth # we will use printf to do this but you could use length and t +he x op # we set the $template to right justified, 8 chars wide column +s # we need to trim the $item to $width -1 chars max if we want +discrete # columns without items touching with no whitespace $item = substr $item, 0, $width -1; printf $width->[$col], $item; } print "\n"; } # get_col_width_templates( 2D_ARRAY_REF, MIN_WHITESPACE ) # takes 2D array ref and integer min whitespace to separate columns # returns an array which contains a printf template such that the # col width is just wide enough for the widest item in that column # aka Excel fit column to data sub get_col_width_templates { my ($array_ref, $min_whitespace ) = @_; $min_whitespace ||= 1; my @width; for my $row ( 0 .. $max_rows -1 ) { for my $col ( 0 .. $col_num -1 ) { my $item = defined $aoa[$col][$row] ? $aoa[$col][$row] : ' +'; $width[$col] = length($item) if length($item) > $width[$co +l]; } } @width = map{ '%' . ( $_ + $min_whitespace) . 's' } @width; return \@width; } __DATA__ one three Just two four another five Perl Hacker !

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Re: Re: Re: Formatting questions by tachyon
in thread Formatting questions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.