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

Error : Illegal octal digit
How do i convert this $item so that no octal error happens
$item = 05540986; #do not change! my @list=qw(05540987 05540984 05540982 05540981 05540986 05540983 055 +40991 05540985 05540990 05540989 05540988 05540992 ); my %lookup;@lookup{@list} = (1..@list); $IDposition = $lookup{$item}; print " $item is located at index $IDposition \n";

Replies are listed 'Best First'.
Re: octal error
by suaveant (Parson) on Sep 26, 2001 at 00:19 UTC
    quote it... $item = '05540986';  #do not change! a leading 0 in an unquoted number in perl makes it octal...
    print "Urk\n" if(777 != 0777);

                    - Ant
                    - Some of my best work - Fish Dinner

      i know but i cant quote it its from a user input! $item = 05540986;# dont change! i must convert this to a string to another variable but how?
        In reply to Gerryjun question on how to keep user input from being interpreted as Octal. in this thread
        my $test=<STDIN>; my $test=sprintf("%s",$test); print $test;


        --mandog
        Well, how does the user input it? It should already be a string if read in from STDIN or anything like that...

                        - Ant
                        - Some of my best work - Fish Dinner

(tye)Re: octal error
by tye (Sage) on Sep 26, 2001 at 00:35 UTC

    Two ways come to mind:

    $item= 5540986; $item= 0 + '05540986';
    The second works because, although 010 is octal when found in Perl source code, when Perl converts a string like '010' to a number, only base 10 is used (otherwise there would be no need for the oct and hex functions).

            - tye (but my friends call me "Tye")
Re: octal error
by VSarkiss (Monsignor) on Sep 26, 2001 at 00:21 UTC

    You just need to quote it: $item = '05540986';  #do not change!Then Perl won't try to interpret it as a number, and the rest of your code will run just fine.

    HTH