Splinter has asked for the wisdom of the Perl Monks concerning the following question:

Here's my problem: I'm using a file to store information about some stuff in delimited form. It is a linked list, so when I get the value of a link it is a string/scalar. I need it to be an integer though. --Currently I use: $link = ord($scalar) - 48; e.g. if $scalar = "4" then $link would end up being 4 This works for the numbers 0 to 9 but once it gets to the number 10 it goes wrong. If $scalar is "10" then $link nds up being 1. ANY ALTERATIVES FOR CHANGING "10" into 10 please.
  • Comment on Getting the Ordinal Value of a string (Kindof)

Replies are listed 'Best First'.
Re: Getting the Ordinal Value of a string (Kindof)
by chipmunk (Parson) on Feb 15, 2001 at 03:25 UTC
    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).

      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.

Re: Getting the Ordinal Value of a string (Kindof)
by Yohimbe (Pilgrim) on Feb 15, 2001 at 03:55 UTC
    Its been said many times on perlmonks that using linked lists in perl is often the application of C programming style to perl. For perl, we seem to idomatically use hashes or lists instead. That being said, the ord call only returns the numeric value of the first character, and since as noted in other posts, text numbers in perl are quite magical in the sense that you can use ++ and other math operations on them and perl just Does What You Meant(tm)
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
Re: Getting the Ordinal Value of a string (Kindof)
by Yoda (Sexton) on Feb 15, 2001 at 05:36 UTC
    One further note, as eluded to in the other responses. The statement ord("4") will not return 4 it will return 52 which is the ascii value for "4".

    Yoda

    UPDATE: My first thought was to delete my reply so I wouldn't look foolish. Then I decided I shoud just be humble and say sorry. I didn't see the "-48" in the original question.