sacky0003
sacky0003

Reputation: 25

Loop is not ending in perl

I want this kind of output:

job cpu-param-14.25.sh 
job gpu-param-14.25.sh
..
..
job cpu-param-15.75.sh 
job gpu-param-15.75.sh

job cpu-param-16.25.sh 
job gpu-param-16.25.sh
..
job cpu-param-18.sh 
job gpu-param-18.sh

This is my code:

#!/usr/bin/perl -w

$incr=0.25;

my $filename = 'job-submit-14.25-18.sh';

open (my $BATCHFILE, '>', "$filename");

$dihed=14.25;
while ($dihed <= 18.0) {
    if ($dihed != 16.0) {
        $dihed += $incr;
    }

    print $BATCHFILE
    "
    job cpu-param-$dihed.sh 
    job gpu-param-$dihed.sh
    "
}

close ($BATCHFILE);

Please help me out.

Upvotes: 0

Views: 50

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

$incr=0.25;
...
$dihed=14.25;
while ($dihed <= 18.0) {
    if ($dihed != 16.0) {
        $dihed += $incr;
    }
    ...
}

This loop starts with $dihed being 14.25 and you increment it in each step by $incr being 0.25. This way $dihed will eventually reach 16.0. This case is explicitly handled in your code so that it does not increase $dihed, which means $dihed will always stay 16.0 from then on and your code will loop forever.

Given the description of what your output should be (i.e. that it should ignore the 16.0) it is more likely that your code should instead always increment but skip the output for 16.0:

$incr=0.25;
...
$dihed=14.25;
while ($dihed <= 18.0) {
    $dihed += $incr;
    if ($dihed == 16.0) {
        next; # skip output for 16.0
    }
    ...
}

Upvotes: 3

Related Questions