Variation on a theme, with a twist that I don't think has been mentioned. I've tried to do my homework, and nothing stood out that would break this. What do you think?

#!/usr/bin/perl -wl use strict; my @numberarray = qw(1 120 13); lengthy(@numberarray); sub lengthy { s{ ( ^ ) } { ( "0" x (6-length $_) ) }xe and print for @_; } __END__

Or, for the commandline:
perl -wle ' s/^/("0"x(6-length))/xe and print for @ARGV;' 133 121 10 13
Or, to make sure that it doesn't zero-pad alphabetical input
perl -wle ' s{(^)(\d+$)}{("0"x(6-length).$2)}xe and print for @ARGV;' 1 fish 002 fish 333 blue fish
And, to print all the arguments, but zero-padding only the numerical arguments:
perl -wle ' s{(^)(\d+$)}{("0"x(6-length).$2)}xe and print or print for @ARGV;' 1 fish 002 fish 333 blue fish

(Sir...put your hands up and please, step away from the keyboard)
One more.. to pad words with spaces, instead of zeros.

perl -wle ' s/^(\d+)/("0"x(6-length).$1)/xe and print or s/^(\w)/("\x20"x(6-length).$1)/xe and print for @ARGV;' 133 121 10 + 13 red

mkmcconn
(I'm gratified by how much better it is than this a few short weeks later. Thanks Perl Monks.).


In reply to Re: Zero Padding by mkmcconn
in thread Zero Padding 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.