in reply to New Perl user - help with my homework
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; print 'Please enter a number: '; chomp( my $limit = <> ); say int(($limit + 1) / 2);
The C-style for loops are randomly used in Perl. Simple while can be used to solve the second task, I tried to show a solution without an array. A little trickery was needed to separate the numbers by spaces.
#! /usr/bin/perl use warnings; use strict; print 'Please enter a number: '; chomp( my $limit = <> ); my $n = $limit + 10; print ' ' x ($n != $limit), $n while ($n -= 10) >= 0; print "\n";
To filter an array, we usually use grep in Perl. Also, I tried to really ask for 4 input values, not 3, not 12.
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; say "Please enter 4 numbers:"; my @numbers; push @numbers, scalar <> for 1 .. 4; chomp(@numbers); say join '-', grep $_ > 50, @numbers;
For the last task, the repetition operator can hide one level of looping:
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; my $size = shift; say '*' x $_ for 1 .. $size;
|
|---|