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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert string variable to integer?

Status
Not open for further replies.

vovan1415

Programmer
Dec 6, 2001
50
US
I am new in Perl language...
How to convert string variable to integer in Perl?

Thanks
 
Perl doesn't differentiate variables by types, only by contexts.

If a scalar-context variable holds something that can be interpreted as a number, perl will do so automatically at need. ______________________________________________________________________
TANSTAAFL!
 
Yep, that's right - but an example may help:

$myVariable = "27822";

$anotherVariable = 23;

$yetAnother = "Hello World";

## Use as a string - add together
$yetAnother = $yetAnother." ".$myVariable;

## The value of $yetAnother is now "Hello World 27822"

## Now lets use it as a number

$myVariable = $myVariable + $anotherVariable;

## The value of $myVariable is now 27845


So long as the variable only contains numbers (and a period in the case of floating point fractions) it can be treated just like a number! One of the beauties of Perl!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top