That's something you'll have to decide. Do you want them padded at the end, or at the beginning? Do you want them padded with empty strings, or zeros? The one thing you shouldn't do is leave them different lengths.

Here's a subroutine you can use to "end pad" arrays with whatever string you prefer... as many arrays as you want.

sub padarrays { my $padchar = shift; my @arrays = @_; my $max = 0; foreach my $aref ( @arrays ) { $max = ( @{$aref} > $max ) ? @{$aref} : $max; } foreach my $aref ( @arrays ) { my $padqty = $max - @{$aref}; next unless $padqty; push @{$aref}, $padchar x $padqty; } }

The sub takes as its first argument a string (it can be empty, a number, or characters) that should be used to pad each array. It takes a list of array refs as the remainder of its parameters. It will then check the length of each array finding the longest, and pad all the rest of the arrays with your pad character so that they're all the same length.

I didn't have time to test it, you may have to tweak a little to get it to work right. caviet emptor.


Dave


In reply to Re: Re: Re: printing arrays by davido
in thread printing arrays 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.