Perl: a perfect squares list

The tutor offers a simple script to demonstrate the Perl range operator.

Let’s imagine someone is studying radicals (which you’ll find numerous posts about here, by typing it in the search box). They want to memorize the perfect squares; a list would be helpful.

Here’s a Perl program that prints the perfect squares from 12 to the number decided by the user:


#!/usr/bin/perl
print “Hello! I print the perfect squares.\n\n”;
print “Enter the number you want to go up to, please\n\n”;
$top=<STDIN>;
print “\nokay…\n\n”;#recall \n means newline
chomp $top;
$i=1;
for(1..$top){#range operator: loops from 1 to $top
print $i*$i.”\n”;# . joins parts of a string together for output
$i++;
}

I’ll be showing more helpful hints about Perl over the summer:)

Source:

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , ,

Leave a Reply