Ah, so it is homework after all :-)!
Even self-imposed homework is homework, but in this case I think you will be a willing participant if I tell you we'll be happy to help you think it through and even tell you once you've got it right, but not to give the answer. :-)
So here is a review of your code:
# *always* use these - they do (some of) your error
# checking for you! - saves *lots* of time
use strict;
use warnings;
#this is right
my $x = 4;
my $y = 4;
for (1..$x) {
#this bit is wrong - see discussion below
print "*" x $y, "\n";
}
The bit I marked wrong is where your problem is. To get
a hole in the middle you need dark space for the edges, i.e. asterisks and white space for the hole, i.e. spaces. If you print "*" x $y you will just get the dark stuff and no white space. So how do you get white space? Ask yourself these questions:
- How many rows do you want to mark the top edge of the box? You can use a solid line of asterisks for each row in the edge since an edge is solid with no whitespace.
- How many rows do you want to mark the bottom edge of the box?
- How many rows are left for the part between the top and bottom edge? If you want to see a hole, each of the lines in the middle will need some white space in it.
- Now inside your for loop, can you think of a way to tell if you are on the top edge, middle, or bottom edge?
- Can you use that to create an if...elsif...else statement that does different things depending on whether you are on the top, middle or bottom?
Printing out the top and bottom is easy: you have that bit already - just print out a solid line of asterisks. But what about the middle? Here's how to think that through:
- How many asterisks do you want for your sides: 1? 2?
- If each of your edges has N asterisks and your total line size is M, how many spaces will you need in the middle?
- Can you think of a way to print out three separate strings on a single line to make a line with a hole in it: one containing N asterisks (left edge), one containing the spaces (middle), and one containing N asterisks (right edge)?
When you've thought this through, update your original post with the changes. If it still isn't working quite right, I'm sure people will be glad to help you further.
Best, beth
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.