A few additional tips. The indenting of your code levels should be consistent. The "smallest foreach loop" should be moved to the left to match the level of the "biggest". However, better would be to combine the 2 loops. Also Perl has a special single action syntax putting the "if" at the end of the statement. This can make the code easier to read with fewer curly braces required.
Another big point: What you name variables DOES matter. @unique does not really describe what that is (these are not the uniques of the original array, this is the consecutive range. I would suggest @consecutive as an alternative name.
use strict;
use warnings;
my @arr = (1,2,2,3,4,6);
sub do_it_all
{
my $biggest = $_[0];
my $smallest = $_[0];
foreach my $num (@_)
{
$biggest = $num if ($num > $biggest);
$smallest = $num if ($num < $smallest);
}
print "\$smallest = $smallest\t\$biggest = $biggest\n";
my @consecutive = ($smallest..$biggest);
print "@consecutive\n";
}
do_it_all(@arr);
__END__
$smallest = 1 $biggest = 6
1 2 3 4 5 6
Update: Instead of "do_it_all", perhaps "conseq_range" or similar would be better?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.