Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: New Perl user - help with my homework

by choroba (Cardinal)
on Dec 25, 2018 at 21:27 UTC ( [id://1227698]=note: print w/replies, xml ) Need Help??


in reply to New Perl user - help with my homework

If you tried to calculate the average from the first task for several input numbers, you might noticed there's no need to sum any numbers. So, I'd implement it as
#! /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;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227698]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found