#How to I convert a string to a number
use strict;
use warnings;
sub atoi {
# Initialize to 0 to avoid "uninitialized value" warnings
my $t = 0;
# Process each character of the first argument
foreach my $d (split(//, shift())) {
$t = $t * 10 + $d;
# Optional: Comment out the next line if you don't want to print intermediate steps
# print "$t\n";
}
return $t;
}
# Declare $number with 'my' to comply with 'use strict'
my $number = atoi("123");
print "string '123' number value is $number\n";
No comments:
Post a Comment