Perl script does not print multiple times

前端 未结 2 477
慢半拍i
慢半拍i 2021-01-29 14:05

I\'ve got this Perl script:

#!/usr/bin/perl
use strict;
use warnings;

print , \"\\n\";
print , \"\\n\";
print , \"\\n\";
         


        
2条回答
  •  抹茶落季
    2021-01-29 14:40

    If you want to loop printing your text an user defined number of times, you can use a do while loop like this:

    #!/usr/bin/env perl
    #Program: stdin.pl
    use strict;
    use warnings;
    use feature 'say'; # like print but automatic ending newline.
    
    my $i = 1; # Our incrementer.
    my $input; # Our text from standard input that we want to loop.
    my $total; # How many times we want to print the text on a loop.
    
    say "Please provide the desired text of your standard input.";
    chomp($input = ); # chomp removes a newline from the standard input. 
    say "Please provide the amount of times that you want to print your standard input to the screen.";
    chomp($total = );
    say ">>> I'm in your loop! <<<"; 
    do # A do while loop iterates at least once and up to the total number of iterations provided by user. 
    {
    say "Iteration $i of text: $input";
    $i++;
    }while($i <= $total);
    say ">>> I'm out of your loop! <<<";
    

    This is what I get when I execute the code with "Hello", and "8" from standard input:

    > perl .\stdin.pl
    Please provide the desired text of your standard input.
    Hello!
    Please provide the amount of times that you want to print your standard input to the screen.
    8
    >>> I'm in your loop! <<<
    Iteration 1 of text: Hello!
    Iteration 2 of text: Hello!
    Iteration 3 of text: Hello!
    Iteration 4 of text: Hello!
    Iteration 5 of text: Hello!
    Iteration 6 of text: Hello!
    Iteration 7 of text: Hello!
    Iteration 8 of text: Hello!
    >>> I'm out of your loop! <<<
    

提交回复
热议问题