I am curious. Standard printf() formats can pad with leading zeroes or leading spaces.
#!/usr/bin/perl -w use strict; printf("%03d\n", 5); printf("%03d\n", 12345); __END__ Prints: 005 12345
What kind of fixed field application do you have that would require padding with other than zero or space? Maybe you are asking how do deal with 12345 above? And the print should be '123' or '345'?

I looked at your site reference http://www.comp.leeds.ac.uk/Perl/control.html#exercise and there is some really bogus advice there! Use chomp() to get rid of trailing LF or CR,LF, not chop()! chop() deletes the last char in the string no matter what it is! chomp() is conditional and only gets rid of trailing "new line" characters. In the case of Windows, chomp() will get rid of 2 characters (the CR and LF), on Unix only one (the LF) - in case of a simple string without any "new line" characters, nothing happens.

Anyway, please explain your application with something other than zero or space leading pad characters. That would be unusual and I'd like to understand it.


In reply to Re: Clever Padding by Marshall
in thread Clever Padding by iweindesmedt

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.