Your question is a bit confusing as you cannot do a numeric operation on a string like
"20 ducks in a row" when warnings and use strict are in force. If the "20" was in a separate variable, once it is used in a numeric context, the string part of that variable doesn't matter anymore. What happens with leading zeroes illustrates this point...
Below, $a starts out as a string with leading zeroes. Adding "0" to it (use in a numeric context), causes the "leading zeroes" to be eliminated, i.e. it is now a binary numeric value. The original string value of $a is now not accessible by any normal Perl operation (you won't know how many leading zeroes it had to begin with). If further math operations are done on $a, no further string to binary conversions are required. Perl math operations are very fast.
Leading zeroes in a numeric assignment like shown below for $b, causes the numeric value to be interpreted as an octal number.
#!/usr/bin/perl -w
use strict;
my $a = '00000022';
my $b = 00000033; #leading zero means octal!
print "$a $b\n"; #00000022 27
$a+=0;
print "$a $b\n"; #22 27 #22 now numeric
Update: -- extraneous verbage that added nothing deleted.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.