A possible solution for the stars.pl
program is as follows:
#!/usr/bin/perl use warnings; use strict; # stars.pl - solution for printing out an increasing number of asterisks, # from 1 to 5. Uses a nested loop structure. For variety, a for() # loop is used on the outside and a while loop is used on the inside. # Many different types of loops could be used in either place. my ($i, $j); for ($i = 1; $i <= 5 ; $i++) { $j = 1; while ($j <= $i) { $j++; } }
Another possible solution is:
#!/usr/bin/perl use warnings; use strict; # stars-foreach.pl - solution for printing out an increasing number # of asterisks, # from 1 to 5. Uses a nested loop structure using # foreach loops. my ($i, $j); foreach $i (1..5) { foreach $j (1..$i) { } }
A third possible solution is:
#!/usr/bin/perl # stars-xop.pl : alternate solution for stars.pl problem # uses the "x" operator which repeats a string (left operator) # a given number (right operator) of times. This avoids the # need for a loop with $j. use warnings; use strict; my $i; for ($i = 1; $i <= 5 ; $i++) { }