perlquestion
AcidHawk
<p>A thousand pardon for this silly question (been away for a while)</p><p>I need to read in a list of numbers and make sure that the numbers start at 1 and increment by 1 until the end of the list. So... <li>1 2 3 4 5 would return a success.</li><li>3 2 1 4 5 would return a success.</li><li>2 3 1 1 6 would return a failure</li><li>1 2 4 5 6 would return a failure</li></p><p>
I have the following code (which works) and was wondering if there is another way of doing this.. I could not find a module on CPAN after a little search earlier today.</p><p>
<code>
#! /usr/bin/perl
use strict;
use warnings;
print "Please enter a list of numbers (I.e. 1 2 3 4 5)\n";
chomp(my $n = <STDIN>);
my @numlist = split/\s/, $n;
@numlist = sort { $a <=> $b } @numlist;
if ($numlist[0] == 1) {
print "Horray Array starts at 1\n";
my $prevnum = 0;
foreach my $number (@numlist) {
unless (($prevnum + 1) == $number) {
print "Error in Sequence";
$prevnum = 0;
last;
}
else {
$prevnum = $number;
}
}
if ($prevnum) {
print "The numbers are correct\n";
}
}
else {
print "Error Array DOES NOT Start at 1\n";
}
</code>
</p><p>P.S. the verbose code is to try and help with readability</p>
-----<BR>
<i>Of all the things I've lost in my life, its my mind I miss the most.</i>