jeudi 3 août 2017

Perl. Possible interpreter error in if-statement?

Perl v5.8.3 (MSWin32-x86). Please see in the bellowed code the three if-statements commented out. Why do they not work? The $n defined on the beginning do the job, but why the statement itself is omitted?

#!/usr/bin/perl -w

my $i = 0;
my $n;

$n = ( $i * 10 ) + 1;

while ( $i < 100 ) {
    #if ( $i ==  $i * 10 + 1 ) {            # doesn't work
    #if ( $i == ( $i * 10 ) + 1 ) {         # doesn't work
    #if ( $i == ( ( $i * 10 ) + 1 ) ) {     # doesn't work
    if ( $i == $n ) {                       # it works
        print( "1. i: " . $i . "\n" );
        $i++;
        next;
    }

    elsif ( $i == $n + 1 ) {                # it works
        print( "2. i: " . $i . "\n" );
        $i++;
        next;
    }

    elsif ( ( $i == $n + 5 ) || ( $i == $n + 6 ) ) {    # it works also
        print( "3. i: " . $i . "\n" );
        $i++;
        next;
    }
    $i++;
}

the result is:

1. i: 1
2. i: 2
3. i: 6
3. i: 7

Aucun commentaire:

Enregistrer un commentaire