note
gmpassos
The best is this 3 questinos, making fun with C and ROMAN Numbers:
<p>
------------------------------------------<br>
How do I convert a string to a number?
<p>
Use this atoi function:
<code>
sub atoi {
my $t;
foreach my $d (split(//, shift())) {
$t = $t * 10 + $d;
}
}
$number = atoi("123");
</code>
------------------------------------------<br>
How do I convert a number to a string?
<p>
Use sprintf:
<code>
$string = sprintf("%f", 123.45);
</code>
<p>
------------------------------------------<br>
How can I tell if a string is a number?
<p>
The simplest method is:
<code>
if ($string == "$string") {
# It is a number
}
</code>
Note the use of the == operator to compare the string to its numeric value. However, this approach is dangerous because the $string might contain arbitrary code such as @{[system "rm -rf /"]} which would be executed as a result of the interpolation process. For safety, use this regular expression:
<code>
if ($var =~ /(?=.)M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})/) {
print "$var contains a number.\b";
}
</code>
<br>
Graciliano M. P.<br>
"The creativity is the expression of the liberty".
<p>
<font size="small"><strong>Edit</strong> [kudra],
2002-09-21
Fixed literal brackets
</font>
</p>
199426
199426