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

Hi, I have a text file with a string of numbers: 1234 The file is named /tmp/count.out. I am trying to get my program to read the file and print out the string, but it is somehow affixing a zero before the string.
#!/usr/bin/perl open (LASTCOUNT, "/tmp/count.out") || die "cannot open the file"; $line = <LASTCOUNT>; close(LASTCOUNT); print $line;
The output is 01234. Where is the preceding 0 coming from? TIA.

Replies are listed 'Best First'.
Re: Zero being added when reading from filehandle?
by Eimi Metamorphoumai (Deacon) on Dec 14, 2004 at 16:30 UTC
    Cutting and pasting exactly from your posted code, I can't reproduce that. Are you absolutely certain that /tmp/count.out file doesn't already have the leading zero? The problem is somewhere in what you're not telling us.
Re: Zero being added when reading from filehandle?
by davido (Cardinal) on Dec 14, 2004 at 16:30 UTC

    How is lastcount being added to the file in the first place? Is there a sprintf involved somewhere in that operation?

    Are you absolutely sure that there is not a zero in the file?


    Dave

Re: Zero being added when reading from filehandle?
by ikegami (Patriarch) on Dec 14, 2004 at 17:05 UTC
    If the 0 is in the file, you can get rid of it by using print 0+$line. (Funny how adding a 0 removes a 0 *grins*. I suppose you could use print $line-0 *grins evilly*.)
Re: Zero being added when reading from filehandle?
by sgifford (Prior) on Dec 14, 2004 at 19:07 UTC
    Maybe count.out doesn't have in it what you think it does? Try looking at it with cat -vE, which should show line endings and control characters; maybe you'll find a character you didn't expect.