0

I want to write a file containing these lines in Perl

YOYO -O -i prod_mdin.150 -o prod_mdout.150  

YOYO -O -i prod_mdin.149.75 -o prod_mdout.149.75  

...

YOYO -O -i prod_mdin.10 -o prod_mdout.10

Perl

print "SrepAring inpCKut filYes\n";

$decr = 0.25;

my $filename = 'report-4.5-3.txt';

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

$dihed = 150.0;

while ( $dihed <= 10.0 ) {

    $dihed -= $decr;

    print $BATCHFILE "
YOYO -O -i min_mdin.${dihed} -o min_mdout.${dihed} 
"
}

print "done\n";

close( $BATCHFILE );
3
  • Your condition while ($dihed <= 10.0) is not true with $dihed=150 so the loop body is not entered at all. Commented Sep 17, 2018 at 7:11
  • More importantly, with while ($dihed <= 10.0) the loop would never terminate (if it was entered somehow). Did you mean while ($dihed >= 10.0) ? Commented Sep 17, 2018 at 7:27
  • 1
    Could you please edit your question to improve the indentation? Good indentation makes code far easier to understand. And if you're asking a large group of strangers to read your code, it's polite to make that as easy as possible. Commented Sep 17, 2018 at 7:36

2 Answers 2

1

Your code does this:

$dihed=150.0;
while ($dihed <= 10.0) {
  $dihed -= $decr;
  ...
}

On the first iteration of your while loop, the Boolean condition fails (as 150.0 is not less than or equal to 10.0). So no code inside the while loop ever gets executed.

Perhaps your Boolean condition should have been $dihed >= 10.0.

Sign up to request clarification or add additional context in comments.

Comments

1

The reason your code isn't working is that your test has the wrong sense: you are starting at 150 and then looping while the value is less than or equal to 10, which is never

It is also a bad idea to repeatedly add floating point values, as the inaccuracies will mount up and result in an error in the output. In this case you are using 0.25, which just happens to be representable accurately as a binary fraction, but in case you ever want a different increment you should know that it is best to use integers to count and convert them to floating point one by one

Finally, you are asking for no decimal places at all on the integers, but two on the fractional values. This may not be absolutely necessary, but it's best to use an explicit format string to avoid some surprises

Here's how I would write your code

use strict;
use warnings 'all';

my $filename = 'report-4.5-3.txt';

open my $out_fh, '>', $filename
        or die qq{Unable to open "$filename" for output: $!};

for ( my $d = 150 * 4; $d >= 10 * 4; --$d ) {

    my $fmt = $d % 4 ? '%.2f' : '%.0f';
    my $dihed = sprintf $fmt, $d / 4;

    print $out_fh "\nYOYO -O -i min_mdin.${dihed} -o min_mdout.${dihed}\n";
}

output

YOYO -O -i min_mdin.150 -o min_mdout.150

YOYO -O -i min_mdin.149.75 -o min_mdout.149.75

YOYO -O -i min_mdin.149.50 -o min_mdout.149.50

YOYO -O -i min_mdin.149.25 -o min_mdout.149.25

YOYO -O -i min_mdin.149 -o min_mdout.149

...

YOYO -O -i min_mdin.11 -o min_mdout.11

YOYO -O -i min_mdin.10.75 -o min_mdout.10.75

YOYO -O -i min_mdin.10.50 -o min_mdout.10.50

YOYO -O -i min_mdin.10.25 -o min_mdout.10.25

YOYO -O -i min_mdin.10 -o min_mdout.10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.