Friday, March 12, 2010

Knit one, Perl two

=comment until cut
Okay, this is a program I did NOT write all by myself.  It is knit together from some sample programs in the tutorials I've been reading.  Like stringing together sentences you've memorized in french before you go off freestyling.  The original program added all the numbers from 1 to 100.  That's neat, but I wanted more flexibility, so I added the power to input whatever two numbers I liked.  It proved a little trickier than I expected - I still can't do it using the 'while' construct; this one uses
'for'.  The construct is the only really fancy part of this program.  And 'fancy' is being used here loosely.  There IS some cool stuff going on... I've got a default variable used for pacing.  I'm pretty pleased.
=cut

$total = 0;
print "Add up all the numbers from X to Y!\n";
print "Hit enter key to continue:\n";
$_  = <STDIN>; 
print "What is the value of X? ";
my $a = <STDIN>; 
chomp($a);
print "Okay, the first number is $a.\n";
print "What is the value of Y? ";
my $b = <STDIN>; 
chomp($b);
print "Okay, the second number is $b.\n";
for $x ( $a .. $b )
{
$total += $x;
}

print "Okay, the total of all numbers from $a to $b is $total\n";

# - J

No comments:

Post a Comment