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

Hi PMs, The output should be "10 & 10 are equal", but this gives output "10 & 8 aren't equal". I don't understand this! perhaps because of leading 0's perl converted 0010 to octel i.e. 8
I tried removing leading 0's, still the same output "10 & 8 aren't equal".

Please help to overcome this.
Actually I'm reading values from a file in which lots of values have leading 0's & I have to compare this value to some other from different file which doesn't have leading 0's.
~csharma
#!/usr/bin/perl $x = 10; $y = 0010; $y =~ s/^0+//; ##OR $y += 0; if($x == $y) { print $x . " and " . $y . " are equal\n"; } else { print $x . " and " . $y . " aren't equal\n"; }

Replies are listed 'Best First'.
Re: removing leading 0's
by AnomalousMonk (Archbishop) on Oct 03, 2015 at 16:06 UTC

    Or looked at another way, by the time you do the substitution, it's too late: there are no leading zeroes to remove.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $y = 0010; print qq{before: '$y'}; ;; $y =~ s/^0+//; print qq{after: '$y'}; " before: '8' after: '8'

    Update: But if you can manage to keep the leading zeroes, as in a string, it all works right.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $y = '010'; print qq{before: '$y'}; ;; $y =~ s/^0+//; print qq{after: '$y'}; print $y + 1; " before: '010' after: '10' 11

    In fact, because a numeric string is always interpreted as decimal base, it works even without removing leading zeroes.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $y = '010'; print qq{before: '$y'}; ;; print $y + 1; " before: '010' 11

    But take care, because as soon as the compiler sees the bare 010, it parses it according to the appropriate number base and it stays that way even if stringized immediately thereafter.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $y = '' . 010; print qq{before: '$y'}; ;; print $y + 1; " before: '8' 9


    Give a man a fish:  <%-{-{-{-<

Re: removing leading 0's
by RichardK (Parson) on Oct 03, 2015 at 15:59 UTC

    OO10 is an octal number equal to 8 (decimal) therefore $y = 8 not 10.

    This may help explain

    perl -E 'say 0010,",","0010";' >8,0010
Re: removing leading 0's
by karlgoethebier (Abbot) on Oct 03, 2015 at 17:01 UTC
    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $x = 10; my $y = 0010; $y = sprintf( "%o", $y ); say $y; if ( $x == $y ) { say qq($x and $y are equal); } else { say qq($x and $y aren't equal); } __END__ karls-mac-mini:monks karl$ ./CSharma.pl 10 10 and 10 are equal

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: removing leading 0's
by BillKSmith (Monsignor) on Oct 04, 2015 at 02:05 UTC
    Try "reading from a file":
    use strict; use warnings; chomp(my $x = <DATA>); chomp(my $y = <DATA>); #$x += 0; if($x == $y) { print $x . " and " . $y . " are equal\n"; } else { print $x . " and " . $y . " aren't equal\n"; } __DATA__ 0010 10
    OUTPUT: 0010 and 10 are equal

    As numbers, they are equal. As strings, they are not! Note that your idea of adding zero would 'fix' the string.

    Bill