Take a look at this script;
#!perl
use strict;use warnings;
use Time::localtime;
print "substr month= " . (substr("0" . localtime->mon() + 1, -2)) . "\
+n";
print "substr day= " . (substr("0" . localtime->mday(), -2)) . "\n";
print "sprintf month= " . (sprintf "%02d", localtime->mon() + 1) . "\n
+";
print "sprintf day= " . (sprintf "%02d", localtime->mday()) . "\n";
It returns the following;
>perl pad.pl
substr month= 1
substr day= 09
sprintf month= 01
sprintf day= 09
Now, I know the first two lines are not a good way to pad. I found some nodes on Perlmonks that showed a better way, which is the last two lines. The problem is I'm perplexed as to the first result. Why does substr month only return a 1 instead of 01?
I'm using ActiveState Perl 5.8.0 running under Win32. Thanks.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|