in reply to Getting the Ordinal Value of a string (Kindof)

Perl will convert between strings and numbers for you. If you assign the string "10" to a variable, and then add 1 to it, the value becomes 11. In Perl, you don't need any special code to make this happen.

This is a fundamental aspect of Perl, and is one of the first things you should learn when you start programming in Perl. You may want to invest in a good Perl book, such as Learning Perl (the Llama book).

  • Comment on Re: Getting the Ordinal Value of a string (Kindof)

Replies are listed 'Best First'.
Re: Re: Getting the Ordinal Value of a string (Kindof)
by sierrathedog04 (Hermit) on Feb 15, 2001 at 17:34 UTC
    Scalar is a basic data type in Perl. There are no basic numeric or basic string data types.

    Perl will interpret a scalar as either a number or a string depending upon its context, but a scalar is both a number and a string at the same time to perl.

    Here is an example of this feature:

    sub testString { my $myVar1 = "12.0"; my $myVar2 = "12"; if ($myVar1 eq $myVar2) { print "String Equality Found\n"; } if ($myVar1 = $myVar2) { print "Numerical Equality Found\n"; } }

    The above subroutine returns "Numerical Equality Found". "eq" is a test for equality between strings, and "=" is a test for equality between numbers.

    Since "12" does not equal "12.0" as a string the test for "eq" fails. Since 12 = 12.0 the test for "=" succeeds. Perl is testing the exact same variables, but it figures out from the context whether to treat those variables as strings or numbers.

    That is why Perl is called a "weakly typed" language. Attempting to do something like the above in C would fail.