I had to do this 3 assignments I finished them I Just wanted to show it to you guys to see if there was anything I Could do to make it better or to shorten some of them Thanks =).
1. Write a subroutine (&evens) that recieves an array of numbers and returns an array containing only even numbers Give an example of calling the subroutine.
#!/usr/bin/perl
use strict;
use warnings;
my@num = (1..10);
sub even
{
foreach my$number(@num)
{
if ($number%2==0)
{
my @evens = $number;
print "@evens";
}
}
}
&even
2. Write a subroutine(&squares) that recieves an array of numbers and squares each number in the array. Note: nothing is returned.
#! /usr/bin/perl
use strict;
use warnings;
my @nums = (1..10);
sub square
{
foreach my$number (@nums)
{
my$number = ($number * $number);
print "$number\n";
}
}
3. Write a subroutine(&huge) that returns true if the number sent is greater than 1,000,000 or false(look up what is true and false). Show an example of calling the function.
#!/usr/bin/perl
use strict;
use warnings;
sub huge
{
my $num = <>;
if ($num > 1000000)
{
print "1";
}
else{
print "0";
}
}
&huge
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|