Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert string to numeric and vice versa

Status
Not open for further replies.

newbie425

Programmer
Jan 27, 2003
16
US
Is it possible to convert a number that is being read in as a string in order to compute the total value? Basically I need to add up the values, then convert the total value back to string.

I can't find any conversion functions in texts nor online.

Thanks
 
Perl doesn't think that way. It is an untyped language.

In perl, variables do not have types, they have contexts. A variable context is "scalar", "array", or "hash". The first is simply a variable that holds only one value. The other two hold multiple values, the difference between them being the types of index keys used -- arrays use numeric keys, hashes alphanumeric. Perl is very good at figuring out what you want to do with a value.

Just sum the strings up. Perl should handle the situation well. Want the best answers? Ask the best questions: TANSTAAFL!
 
Code:
my $string1 = "123";
my $string2 = "456";

my $string3 = $string1 + $string2; # sums to 579
my $string4 = $string1 . $string2; # concatenates to "123456"
my $string5 = $string3 . $string4; # concatenates to "579123456"

print $string3; # outputs: 579
print $string4; # outputs: 123456
print $string5; # outputs: 579123456
Perl does the conversions for you automatically... that's just one of the things that is so awesome about this language. Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top